INF1511 - Chapter 5 - Classes
INF1511 - Chapter 5 - Classes
INF1511 - Chapter 5 - Classes
The attributes of a class are objects that are contained in the body of the class (as defined in
the statements) or are built-in attributes. Refer to page 132 of the textbook for built-in class
attributes.
class classname(base-classes):
class variable(s)
def method 1(
statement(s) self):
instance variable(s)
To create an instance of a class called classname, you call the class object as if it were without
parameters as shown below:
r=classname()
Arguments can be passed to the __init__ using the following statement def __init__ (self, x,y)
5. Class Methods
A class method has no self argument and receives a class (called cls) as its first argument.
The class method is defined as follows:
@classmethod
Def f (cls, parm1, parm2, ….):
body of method
where warm1, parm2 … are parameters of the object.
6. Static Methods
Static methods are ordinary functions that bind their results to a class attribute. It does not
have a self or cls parameter and is automatically inherited by any child classes. The static
method is immutable via inheritance.
@staticmethod
Def name (parm …):
body of the method
7. Garbage Collection
Garbage collection is a procedure for freeing up the memory that is used by the variables or
instances that are no longer required.
Python uses a reference count, any object whose reference count is zero is garbage collected.
8. Inheritance
Inheritance is a technique of copying the data members and member functions of an existing
class into another class.
The members in a class can be defined as:
Public – accessed from within or outside of the class.
Private – can be accessed from outside of the class.
Member functions in the inheriting class that have the same name as the base class’ function
will override the base class’ function, i.e. the function in the sub class will be executed. This is
called method overriding.
The methods of a base class can be called from the sub class by using fully qualified method
names, i.e. base_classname.methodname.
i. Single inheritance – one class is derived from another single class, implemented as
follows:
Class sub-class-name (base-class-name)
ii. Multilevel inheritance – when a class inherits a class that in turn is inherited by some
other class.
iii. Multiple Inheritance – one class inherits from more than one base class. If two classes
with the same function name are inherited from, the function from the first base class
will be executed.
9. Operator overloading
Overloading is when arithmetic operators are used to perform operations on classes.
The comparison operator == can be used to determine if two instances have instance variables with
the same value.
Polymorphism allows different methods with the same name in different classes to perform
different tasks.
Properties are used to manage attributes with get and set methods.
10. Descriptors
Descriptors are used to manage instance attributes. There are two types of descriptors;
i. Non-data descriptors – implements the _get_ method.
ii. Data descriptor – implements the _delete_ , _set_ and _get_ methods.
The __setattr__ method is called whenever you assign a value to an instance variable
The __getattr__method fetches an attribute of an instance using a string object and is called when
attribute look up fails, that is, when you access an undefined attribute.