Learn Python 3 - Classes Cheatsheet - Codecademy
Learn Python 3 - Classes Cheatsheet - Codecademy
Classes
Python repr method
The Python __repr__() method is used to tell
Python what the string representation of the class should class Employee:
be. It can only have one parameter, self , and it should def __init__(self, name):
return a string. self.name = name
def __repr__(self):
return self.name
john = Employee('John')
print(john) # John
# Class Instantiation
ferrari = Car()
/
Python Class Variables
In Python, class variables are de ned outside of all
methods and have the same value for every instance of class my_class:
the class. class_variable = "I am a Class
Class variables are accessed with the Variable!"
instance.variable or
class_name.variable syntaxes. x = my_class()
y = my_class()
print(x.class_variable) #I am a Class
Variable!
print(y.class_variable) #I am a Class
Variable!
dog = Animal('Woof')
print(dog.voice) # Output: Woof
a = 1.1
print(type(a)) # <class 'float'>
a = 'b'
print(type(a)) # <class 'str'>
a = None
print(type(a)) # <class 'NoneType'>
/
Python class
In Python, a class is a template for a data type. A class can
be de ned using the class keyword. # Defining a class
class Animal:
def __init__(self, name,
number_of_legs):
self.name = name
self.number_of_legs = number_of_legs
print(dir())
# ['Employee', '__builtins__', '__doc__',
'__file__', '__name__', '__package__',
'new_employee']
print(dir(Employee))
# ['__doc__', '__init__', '__module__',
'print_name']
__main__ in Python
In Python, __main__ is an identi er used to reference
the current le context. When a module is read from
standard input, a script, or from an interactive prompt, its
__name__ is set equal to __main__ .
Suppose we create an instance of a class called
CoolClass . Printing the type() of the instance
will result in:
<class '__main__.CoolClass'>
/
Super() Function in Python Inheritance
Python’s super() function allows a subclass to invoke
its parent’s version of an overridden method. class ParentClass:
def print_test(self):
print("Parent Method")
class ChildClass(ParentClass):
def print_test(self):
print("Child Method")
# Calls the parent's version of
print_test()
super().print_test()
child_instance = ChildClass()
child_instance.print_test()
# Output:
# Child Method
# Parent Method
Polymorphism in Python
When two Python classes o er the same set of methods
with di erent implementations, the classes are class ParentClass:
polymorphic and are said to have the same interface. An def print_self(self):
interface in this sense might involve a common inherited print('A')
class and a set of overridden methods. This allows using
the two objects in the same way regardless of their
class ChildClass(ParentClass):
individual types.
def print_self(self):
When a child class overrides a method of a parent class,
print('B')
then the type of the object determines the version of the
method to be called. If the object is an instance of the
child class, then the child class version of the overridden obj_A = ParentClass()
method will be called. On the other hand, if the object is obj_B = ChildClass()
an instance of the parent class, then the parent class
version of the method is called. obj_A.print_self() # A
obj_B.print_self() # B
/
Dunder methods in Python
Dunder methods, which stands for “Double Under”
(Underscore) methods, are special methods which have class String:
double underscores at the beginning and end of their # Dunder method to initialize object
names. def __init__(self, string):
We use them to create functionality that can’t be self.string = string
represented as a normal method, and resemble native
Python data type interactions. A few examples for dunder
string1 = String("Hello World!")
methods are: __init__ , __add__ , __len__ ,
print(string1.string) # Hello World!
and __iter__ .
The example code block shows a class with a de nition
for the __init__ dunder method.
Python Inheritance
Subclassing in Python, also known as “inheritance”, allows
classes to share the same attributes and methods from a class Animal:
parent or superclass. Inheritance in Python can be def __init__(self, name, legs):
accomplished by putting the superclass name between self.name = name
parentheses after the subclass or child class name.
self.legs = legs
In the example code block, the Dog class subclasses
the Animal class, inheriting all of its attributes. class Dog(Animal):
def sound(self):
print("Woof!")
Yoki = Dog("Yoki", 4)
print(Yoki.name) # YOKI
print(Yoki.legs) # 4
Yoki.sound() # Woof!
/
+ Operator
In Python, the + operation can be de ned for a user-
class A:
de ned class by giving that class an .__add()__
method.
def __init__(self, a):
self.a = a
def __add__(self, other):
return self.a + other.a
obj1 = A(5)
obj2 = A(10)
print(obj1 + obj2) # 15
class Member(Family):
def type(self):
print("Child class")