You are on page 1of 12

Python Advance

Classes and Instances


• At the class level, variables are referred to as class
variables, whereas variables at the instance level
are called instance variables.
• Class variables are defined within the class
construction.
• Instance variables are owned by instances of the
class.
• This means that for each object or instance of a
class, the instance variables are different.
• Instance variables are defined within methods.
Inheritance
• Have a Derived class that inherits from
a Base class.
• Inheritance is represented using the Unified
Modeling Language.
• Classes that inherit from another are called
derived classes, subclasses, or subtypes.
• Classes from which other classes are derived are
called base classes or super classes.
• A derived class is said to derive, inherit, or extend
a base class.
Composition
• It models a has a relationship.
• It enables creating complex types by
combining objects of other types.
Class vs Static Method
• A class method is a method which is bound to
the class and not the object of the class.
• It can modify a class state
• A class method takes cls as first parameter
while a static method needs no specific
parameters.
• @classmethod decorator - a class method
@staticmethod decorator-a static method
Operator Overloading
• Means giving extended meaning beyond their
predefined operational meaning.
• Same built-in operator or function shows
different behaviour for objects of different
classes, this is called Operator Overloading.
Polymorphism
• Means the ability to take various forms
• Allows us to define methods in the child class
with the same name as defined in their parent
class.
Iterators
• Any python type that can be used with a for in
loop.
• Python lists, tuples, dicts and sets are all
examples of inbuilt iterators.
• _iter__ method that is called on initialization of
an iterator. This should return an object that has
a next or __next__ (in Python 3) method.
• next ( __next__ in Python 3) -The iterator next
method should return the next value for the
iterable.
Decorators
• Modify the behaviour of function or class.
• Allow us to wrap another function in order to
extend the behaviour of wrapped function,
without permanently modifying it.
• In Decorators, functions are taken as the
argument into another function and then
called inside the wrapper function.
Generators
• There are two terms involved:
• Generator-Function:
• whenever it needs to generate a value, it does
so with the yield keyword rather than return.
• If the body of a def contains yield, the
function automatically becomes a generator
function.
• Generator-Object:
• Generator functions return a generator object.
• Generator objects are used either by calling
the next method on the generator object .
• using the generator object in a “for in” loop.

You might also like