Python Class and Objects

Share:

Class

Class is a blueprint for its objects, classes can have methods, attributes and special method (like ( __init__ ) constructor), here we will see the class general structure in python:

                class ClassName ():
                                Attributes
                                Constructor (__init__ method)
                                Methods

To create a class, we need the class keyword following by its name, prentices and colon followed by the class body at the end.



Attributes:
Attributes are simply variables that can store the data about objects, we can create as many as attributes that we want, and then use them to perform operation on our object.

Methods:
Methods are functions in fact but inside a class they are called methods but methods have one main deference with normal function methods have a (self) keyword before its parameters inside of  it prentices:

                def FuncName (self , parameter, …): codes

(self) keyword connects a method with the class so each class method including constructor must have (self) keyword at the beginning of its parameters, creating method parameter is optional, in order to make a default value for method parameters we need to initialize them this method is useful with constructors.

Note: with (self) keyword we can access the class attributes just by putting a dot after the (self) keyword and writing attribute name inside a method including constructors:

                self.VarName

Constructor:
A constructor is a special method with (__init__) name which allow us to create class object instances, constructor is usually used for initializing class attributes:

                def __init__ (self , parameter, …): codes


object

objects are instance of the class which have access to all class attributes and methods, objects can be stored into variables:

                ObjName = ClassName(Value, …)

If the constructor of the class requires values for its parameters we have to pass its values while creating class instances, if we initialized the constructor parameters we can leave their value places empty it will use the default values.
After creating an object and storing it into a variable we can access the attributes and methods by putting a dot after the variable name:

                ObjName.AttributeName                            # getting object attribute value
                ObjName.AttributeName = Value            # setting object attribute value
                ObjName.FuncName(value, …)                 # accessing object methods

Polymorphism

Treating objects more generally is called polymorphism, python makes polymorphism easy for us and the reason is type flexibility.

                def FuncName (VarName):codes

we have a simple function above we can pass any type and do anything that we want, like imagine we have two class with methods which have the same name, and we call that method inside a function, if we pass both classes object into that function it will work perfectly and we applied polymorphism.

Inheritance

Inheritance helps us to access the parent class elements into child class:

                class parent (): codes
                class child (parent): codes

when we inherit from a class we can override methods including constructors if we don’t write any constructor for our child class python will use the parent class constructor.
Beside overriding the parent class methods, we can add new methods and attributes into subclass.

Note: rewriting the same class which exist in the parent class but with deferent functionality is called overriding.

Classes special methods:
We can write some special methods to make our classes more dynamic, like using print, len or del methods special methods are pre and sub fixed with double underscore (__Name__):
Print: To use print method with our class we need to write its special method inside the target class:

                def __str__ (self): codes

len: we can use len with our classes too but we must write its special method before using it:

                def __len__ (self): codes

del: same with above special method in order to use del we need to write it inside our class first:

                def __del__ (self): codes

Note: what these methods do is depend on what we write for it.

No comments