@vinaymavi

Agenda

  • Decorators 
  • Class
  • Encapsulation 
  • Inheritance 

Decorators

Function

Functions are first class object in python and can be passed as argument and return as value.

Decorators

def parent(child):
    print "Before child"

    def child_proxy():     
        print "Child Proxy Start"
        child()
        print "Child Proxy End"        
    print "Before child"

    return child_proxy


@parent
def child():
    print "Child Calling..."

# parent(child)

child()
def parent(child):
    print "Before child"

    def child_proxy():     
        print "Child Proxy Start"
        child()
        print "Child Proxy End"        
    print "Before child"

    return child_proxy



def child():
    print "Child Calling..."

# parent(child)

parent(child)()

Class

class <class_name>:
    <class_defination>
class MyClass:
    def my_name(self):
        print "My name is 'My Class'"

Class

class MyClass:
    def __init__(self):
        print "Init calling..."

Constructor

Class

class MyClass:
    def __private(self):
        print "I am private"
    def public(self):
        print "I am public"

Class Members

class MyClass:
    def __init__(self, name):
        self.__private_name = "LOL"
        self.name = name
    
    def my_name(self):
        print self.__private_name     

Static Variables

Class

Static Variable

class MyClass:
    CLASS_NAME = "My Class"    
    def my_name(self):
        print MyClass.CLASS_NAME

my_class = MyClass()
my_class.my_name()

Class

Static Methods

class MyClass:
    CLASS_NAME = "My Class"
    @staticmethod
    def my_name():
        print MyClass.CLASS_NAME

MyClass.my_name()

Class

Class Methods

class MyClass:
    MY_NAME = "My Class"
    @classmethod
    def print_my_name(cls):
        print cls.MY_NAME

Encapsulation

Inheritance

Inheritance

class <class_name>(<Base_Class>):
    <class_body>
class Person:
    def __init__(self, *args, **kwargs):
        self.name = "Person"
    def print_name(self):
        print self.name

class Employee(Person):
    def __init__(self, *args, **kwargs):
            Person.__init__(self)
            self.company_name = "Sapient"

    def print_company(self):
        print self.company_name

emp = Employee()
emp.print_name()
emp.print_company()

Inheritance

class Person(object):
    def __init__(self, *args, **kwargs):
        self.name = "Person"
    def print_name(self):
        print self.name

class Employee(Person):
    def __init__(self, *args, **kwargs):
            super(Employee,self).__init__()
            self.company_name = "Sapient"

    def print_company(self):
        print self.company_name

emp = Employee()
emp.print_name()
emp.print_company()

Multiple Inheritance

class <class_name>(<base_class_1>,<base_class_2>):
    <class_body>

Multiple Inheritance

class Person(object):
    def __init__(self, *args, **kwargs):  
        print "Person calling." 
        super(Person,self).__init__()             
        self.name = "Person"
    def print_name(self):
        print self.name
class Developer(object):
    def __init__(self, *args, **kwargs):
        super(Developer,self).__init__()     
        print "Developer calling."
        self.type_of_developer = "Front-End"

    def print_developer_type(self):
        print self.type_of_developer

class Employee(Person, Developer):
    def __init__(self, *args, **kwargs):
            print "Employee calling."
            super(Employee, self).__init__()
            self.company_name = "Sapient"

    def print_company(self):
        print self.company_name
print Employee.__mro__
emp = Employee()
emp.print_name()
emp.print_company()
emp.print_developer_type()

Thank you.

Made with Slides.com