Learn Python 3 - Classes Cheatsheet - Codecademy
Learn Python 3 - Classes Cheatsheet - Codecademy
Classes
The Python __repr__() method is used to tell Python what the string representation class Employee:
of the class should be. It can only have one parameter, self , and it should return a
def __init__(self, name):
string.
self.name = name
def __repr__(self):
return self.name
john = Employee('John')
print(john) # John
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 1/6
09/12/2023, 08:05 Learn Python 3: Classes Cheatsheet | Codecademy
In Python, methods are functions that are defined as part of a class. It is common # Dog class
practice that the first argument of any method that is part of a class is the actual
class Dog:
object calling the method. This argument is usually called self.
# Method of the class
def bark(self):
print("Ham-Ham")
# Class Instantiation
ferrari = Car()
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 2/6
09/12/2023, 08:05 Learn Python 3: Classes Cheatsheet | Codecademy
In Python, class variables are defined outside of all methods and have the same value class my_class:
for every instance of the class.
class_variable = "I am a Class Variable!"
Class variables are accessed with the instance.variable or class_name.variable
syntaxes.
x = my_class()
y = my_class()
In Python, the .__init__() method is used to initialize a newly created object. It is class Animal:
called every time the class is instantiated.
def __init__(self, voice):
self.voice = voice
dog = Animal('Woof')
print(dog.voice) # Output: Woof
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 3/6
09/12/2023, 08:05 Learn Python 3: Classes Cheatsheet | Codecademy
The Python type() function returns the data type of the argument passed to it. a = 1
print(type(a)) # <class 'int'>
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 defined using the class # Defining a class
keyword.
class Animal:
def __init__(self, name, number_of_legs):
self.name = name
self.number_of_legs = number_of_legs
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 4/6
09/12/2023, 08:05 Learn Python 3: Classes Cheatsheet | Codecademy
In Python, the built-in dir() function, without any argument, returns a list of all the class Employee:
attributes in the current scope.
def __init__(self, name):
With an object as argument, dir() tries to return all valid object attributes.
self.name = name
def print_name(self):
print("Hi, I'm " + self.name)
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 identifier used to reference the current file 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'>
This means that the class CoolClass was defined in the current script file.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 5/6
09/12/2023, 08:05 Learn Python 3: Classes Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet 6/6