INF1511 - Chapter 5 - Classes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

INF 1511 – Revision notes – Chapter 5

1. The class statement


A class is a template or blueprint for data and operations. It consists of the following:
i. Data members – consisting of class variables (outside of any method of the class) and
instance variables which are inside a method.
ii. Member functions – operations that can be applied to the data members.
A class is created using the following syntax:
class classname (base-classes):
statement(s)
The base-class above is the class from which the current class inherits from, it is also called a
super class.
The inheriting class is called a derived class or a subclass.
The statements are called the body of the class.

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.

2. Defining functions in a class


Functions defined in a class are known as methods and they always start with the parameter
named self. These methods are called instance methods

Instance methods are defined as follows:

class classname(base-classes):
class variable(s)
def method 1(
statement(s) self):
instance variable(s)

3. Accessing Class Variables in Instance Methods


To access class variables, the methods defined in a class body must use a fully qualified name;
the class object must be fully prefixed with the class variables.
4. Instances
An instance is a variable that acts as a replica of a class and an unlimited number of instances
can be created and each gets a separate copy of the methods and attributes defined in the
class.

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()

The _ _init_ _() method is used to initiate the class.

Python allows for the assigning of one instance to another.


Inst1 = inst2
This will create the instance if it does not exist and all the variables in inst1 will be set to the
values in inst2.

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.

The static method is defined as follows:

@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.

You might also like