Python OOP Terminology
Python OOP Terminology
Description Example
Class A blueprint to create objects. It defines the data (attributes) and functionality (
class Dog:
methods) of the objects. You can access both attributes and methods via the dot
notation. # class attribute
is_hairy = True
Object A piece of encapsulated data with functionality in your Python program that is # constructor
(=instance) built according to a class definition. Often, an object corresponds to a thing in the
def __init__(self, name):
real world. An example is the object "Obama" that is created according to the class
definition "Person". An object consists of an arbitrary number of attributes and # instance attribute
methods, encapsulated within a single unit. self.name = name
Instantiation
The process of creating an object of a class. This is done with the constructor # method
method __init__(self, …). def bark(self):
Method print("Wuff")
A subset of the overall functionality of an object. The method is defined similarly
bello = Dog("bello")
to a function (using the keyword "def") in the class definition. An object can have
paris = Dog("paris")
an arbitrary number of methods.
print(bello.name)
Self "bello"
The first argument when defining any method is always the self argument. print(paris.name)
"paris"
This argument specifies the instance on which you call the method.
self gives the Python interpreter the information about the concrete instance.
To define a method, you use self to modify the instance attributes. But to
class Cat:
call an instance method, you do not need to specify self.
Instance A variable that holds data that belongs only to a single instance. Other instances do not
attribute # Dynamic attribute
share this variable (in contrast to class attributes). In most cases, you create an instance
(=instance fifi.likes = "mice"
attribute x in the constructor when creating the instance itself using the self keywords (e.g.
variable) print(fifi.likes)
self.x = <val>).
"mice"
# Inheritance
Dynamic
An instance attribute that is defined dynamically during the execution of the program and
attribute class Persian_Cat(Cat):
that is not defined within any method. For example, you can simply add a new attribute
neew to any object o by calling o.neew = <val>. classification = "Persian"
mimi = Persian_Cat()
Method print(mimi.miau(3))
overloading You may want to define a method in a way so that there are multiple options to call
it. For example for class X, you define a method f(...) that can be called in three "miau miau miau "
ways: f(a), f(a,b), or f(a,b,c). To this end, you can define the method with default
print(mimi.classification)
parameters (e.g. f(a, b=None, c=None).
Inheritance Class A can inherit certain characteristics (like attributes or methods) from class B. For
example, the class "Dog" may inherit the attribute "number_of_legs" from the class
MD. Aliul Islam
"Animal". In this case, you would define the inherited class "Dog" as follows: "class 01518317509
Dog(Animal): ..."