Wednesday, October 24, 2018

Intermediate Object Oriented Python


Class

In [1]:
class Employee:
    def __init__(self, first,last, sal):
        self.first = first
        self.last = last
        self.sal = sal
    def fullname(self):
        return '{} {}'.format(self.first,self.last)
In [2]:
emp1 = Employee("arun","pra", 10)
In [3]:
emp2 = Employee("bal", "cm", 20)
In [4]:
emp1.fullname()
Out[4]:
'arun pra'
In [5]:
Employee.fullname(emp1)
Out[5]:
'arun pra'

Class variables

In [6]:
class Employee:
    def __init__(self, first,last, sal):
        self.first = first
        self.last = last
        self.sal = sal
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    def increment(self):
        self.sal = self.sal *1.05
In [7]:
emp1 = Employee("arun","pra", 10)
emp2 = Employee("bal", "cm", 20)
In [8]:
emp1.sal
Out[8]:
10
In [9]:
emp1.increment()
emp1.sal
Out[9]:
10.5
In [10]:
class Employee:
    incre_val = 1.05
    n_emp = 0
    def __init__(self, first,last, sal):
        self.first = first
        self.last = last
        self.sal = sal
        
        Employee.n_emp += 1
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    def increment(self):
        self.sal = self.sal *self.incre_val
In [11]:
print(Employee.n_emp)
emp1 = Employee("arun","pra", 10)
emp2 = Employee("bal", "cm", 20)
print(Employee.n_emp)
0
2
In [12]:
emp1.sal
Out[12]:
10
In [13]:
emp1.increment()
emp1.sal
Out[13]:
10.5
In [14]:
emp2.incre_val = 1.08
In [15]:
emp2.sal
Out[15]:
20
In [16]:
emp2.increment()
emp2.sal
Out[16]:
21.6
In [17]:
emp2.incre_val
Out[17]:
1.08

@classmethod, @staticmethod

In [18]:
class Employee:
    def __init__(self,first, last, sal):
        self.first, self.last, self.sal = first, last, sal
    def firstname(self):
        return '{} {}'.format(self.first, self.last)
In [19]:
e1 = Employee("arun","pra", 1000)
In [20]:
e1.sal
Out[20]:
1000
New usecase: create Employee from string "arun_prakash_1000"
In [21]:
emp_str = "prakash_pr_2000"
first,last,sal = emp_str.split("_")
e2 = Employee(first,last,sal)
In [22]:
e2.sal
Out[22]:
'2000'
Let's add the above functionality in the Employee class
In [23]:
class Employee:
    def __init__(self,first, last, sal):
        self.first, self.last, self.sal = first, last, sal
    def firstname(self):
        return '{} {}'.format(self.first, self.last)
    def ch(self,emp_str):
        self.first,self.last,self.sal = emp_str.split("_")
        
In [24]:
e1 = Employee.ch(emp_str)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-5a98d16ef119> in <module>()
----> 1 e1 = Employee.ch(emp_str)

TypeError: ch() missing 1 required positional argument: 'emp_str'
In [25]:
class Employee:
    def __init__(self,first, last, sal):
        self.first, self.last, self.sal = first, last, sal
    def firstname(self):
        return '{} {}'.format(self.first, self.last)
    @classmethod
    def from_string(cls,emp_str):
        first,last,sal = emp_str.split("_")
        return cls(first,last,sal)
In [26]:
emp_str = "prakash_pr_9000"
new_emp = Employee.from_string(emp_str)
In [27]:
new_emp.sal
Out[27]:
'9000'
In [28]:
import datetime
d = datetime.date(2016,10,7)
In [29]:
d
Out[29]:
datetime.date(2016, 10, 7)

staticmethod

In [30]:
class Employee:
    def __init__(self,first, last, sal):
        self.first, self.last, self.sal = first, last, sal
    def firstname(self):
        return '{} {}'.format(self.first, self.last)
    @classmethod
    def from_string(cls,emp_str):
        first,last,sal = emp_str.split("_")
        return cls(first,last,sal)
    @staticmethod
    def isworkday(day):
        if day.weekday() == 5 or day.weekday() == 6:
            return True
        else:
            return False

        
In [31]:
emp1 = Employee("arun","prak", 10000)
In [32]:
import datetime
d = datetime.date(2016,10,8)
print(d.weekday())
emp1.isworkday(d)
5
Out[32]:
True

Inheritance

In [33]:
class Employee:
    pay_r = 1.04
    def __init__(self, first, last, sal):
        self.first, self.last, self.sal = first, last, int(sal)
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    def pay_raise(self):
        self.sal = self.sal * self.pay_r
        
    @classmethod
    def cls_from_string(cls,emp_str):
        first,last,sal = emp_str.split("_")
        return cls(first,last,sal)
    @staticmethod
    def check():
        return 4
        
In [34]:
emp1 = Employee.cls_from_string("arun_prakash_10000")
In [35]:
emp1.fullname()
Out[35]:
'arun prakash'
In [36]:
print(emp1.sal)
emp1.pay_raise()
print(emp1.sal)
10000
10400.0
In [37]:
class Developer(Employee):
    pass
    
In [38]:
d1 = Developer("Arun", "Prak", 20000)
In [39]:
d1.fullname()
Out[39]:
'Arun Prak'
In [40]:
class Developer(Employee):
    def __init__(self,first,last,sal, lang):
        super().__init__(first,last,sal)
        self.lang = lang
In [41]:
d2 = Developer("arun", "praks", 20000, "python")
In [42]:
d2.lang, d2.sal
Out[42]:
('python', 20000)
In [43]:
d2.pay_raise()
d2.sal
Out[43]:
20800.0
In [44]:
class Developer(Employee):
    pay_r = 1.2
    def __init__(self,first,last,sal, lang):
        super().__init__(first,last,sal)
        self.lang = lang
d2 = Developer("arun", "praks", 20000, "python")
In [45]:
d2.sal
Out[45]:
20000
In [46]:
d2.pay_raise()
In [47]:
d2.sal
Out[47]:
24000.0
In [48]:
isinstance(d2,Employee)
Out[48]:
True
In [49]:
isinstance(d2, Developer)
Out[49]:
True
In [50]:
issubclass(Developer, Employee)
Out[50]:
True

Dunder methods

In [51]:
class Employee:
    def __init__(self, first, last, sal):
        self.first,self.last,self.sal = first,last,sal
    def fullname(self):
        return '{} {}'.format(self.first,self.last)
    
In [52]:
e1 = Employee("Arun","Prakash",10000)
In [53]:
e1.fullname()
Out[53]:
'Arun Prakash'
In [54]:
print(e1)
<__main__.Employee object at 0x11072df60>
In [55]:
str(e1)
Out[55]:
'<__main__.Employee object at 0x11072df60>'
In [56]:
class Employee:
    def __init__(self, first, last, sal):
        self.first,self.last,self.sal = first,last,sal
    def fullname(self):
        return '{} {}'.format(self.first,self.last)
    def __repr__(self):
        return 'Employee( {}, {} ,{})'.format(self.first,self.last,self.sal)
    def __str__(self):
        return self.fullname()
    def __add__(self,e):
        return self.sal + e.sal
    def __len__(self):
        return len(self.fullname())
In [57]:
e1 = Employee("Arun","Prakash",10000)
print(e1)
Arun Prakash
In [58]:
len(e1)
Out[58]:
12
In [59]:
e2 = Employee("ar","pr", 20000)
In [60]:
e1+e2
Out[60]:
30000

@property, setter, getter, deleter

In [61]:
class Employee:
    def __init__(self, first,last,sal):
        self.first, self.last, self.sal = first,last,sal
        self.email = self.first+"."+self.last +"@email.com"
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    
In [62]:
e1 = Employee("arun","prak", 10000)
In [63]:
e1.fullname()
Out[63]:
'arun prak'
In [64]:
e1.email
Out[64]:
'arun.prak@email.com'
In [65]:
print(e1.first)
arun
In [66]:
e1.first = "Arun"
In [67]:
print(e1.first)
print(e1.email)
print(e1.last)
Arun
arun.prak@email.com
prak
In [68]:
class Employee:
    def __init__(self, first,last,sal):
        self.first, self.last, self.sal = first,last,sal
    @property
    def email(self):
        return self.first+"."+self.last +"@email.com"
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    
In [69]:
e1 = Employee("arun","prak", 10000)
print(e1.first)
print(e1.email)
print(e1.last)
arun
arun.prak@email.com
prak
In [70]:
e1.first = "Arun"
In [71]:
print(e1.first)
print(e1.email)
print(e1.last)
Arun
Arun.prak@email.com
prak
In [72]:
class Employee:
    def __init__(self, first,last,sal):
        self.first, self.last, self.sal = first,last,sal
    @property
    def email(self):
        return self.first+"."+self.last +"@email.com"
    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    
In [73]:
e1 = Employee("arun","prak", 10000)
In [74]:
print(e1.first)
print(e1.fullname)
print(e1.last)
arun
arun prak
prak
In [75]:
e1.fullname = "Arun Prakash"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-75-0582b7cda382> in <module>()
----> 1 e1.fullname = "Arun Prakash"

AttributeError: can't set attribute
In [81]:
class Employee:
    def __init__(self, first,last,sal):
        self.first, self.last, self.sal = first,last,sal
    @property
    def email(self):
        return self.first+"."+self.last +"@email.com"
    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)
    @fullname.setter
    def fullname(self,name):
        first,last = name.split(" ")
        self.first = first
        self.last = last
    @fullname.deleter
    def fullname(self):
        print("Deleting the name")
        self.first = None
        self.last = None
    
In [82]:
e1 = Employee("arun","prak", 10000)
In [83]:
print(e1.first)
print(e1.fullname)
print(e1.last)
arun
arun prak
prak
In [84]:
e1.fullname = "Arun Prakash"
In [85]:
print(e1.first)
print(e1.fullname)
print(e1.last)
Arun
Arun Prakash
Prakash
In [86]:
del e1.fullname
Deleting the name
In [87]:
print(e1.first)
print(e1.fullname)
print(e1.last)
None
None None
None

No comments :

Post a Comment