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]:
In [5]:
Employee.fullname(emp1)
Out[5]:
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]:
In [9]:
emp1.increment()
emp1.sal
Out[9]:
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)
In [12]:
emp1.sal
Out[12]:
In [13]:
emp1.increment()
emp1.sal
Out[13]:
In [14]:
emp2.incre_val = 1.08
In [15]:
emp2.sal
Out[15]:
In [16]:
emp2.increment()
emp2.sal
Out[16]:
In [17]:
emp2.incre_val
Out[17]:
@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]:
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]:
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)
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]:
In [28]:
import datetime
d = datetime.date(2016,10,7)
In [29]:
d
Out[29]:
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)
Out[32]:
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]:
In [36]:
print(emp1.sal)
emp1.pay_raise()
print(emp1.sal)
In [37]:
class Developer(Employee):
pass
In [38]:
d1 = Developer("Arun", "Prak", 20000)
In [39]:
d1.fullname()
Out[39]:
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]:
In [43]:
d2.pay_raise()
d2.sal
Out[43]:
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]:
In [46]:
d2.pay_raise()
In [47]:
d2.sal
Out[47]:
In [48]:
isinstance(d2,Employee)
Out[48]:
In [49]:
isinstance(d2, Developer)
Out[49]:
In [50]:
issubclass(Developer, Employee)
Out[50]:
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]:
In [54]:
print(e1)
In [55]:
str(e1)
Out[55]:
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)
In [58]:
len(e1)
Out[58]:
In [59]:
e2 = Employee("ar","pr", 20000)
In [60]:
e1+e2
Out[60]:
@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]:
In [64]:
e1.email
Out[64]:
In [65]:
print(e1.first)
In [66]:
e1.first = "Arun"
In [67]:
print(e1.first)
print(e1.email)
print(e1.last)
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)
In [70]:
e1.first = "Arun"
In [71]:
print(e1.first)
print(e1.email)
print(e1.last)
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)
In [75]:
e1.fullname = "Arun Prakash"
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)
In [84]:
e1.fullname = "Arun Prakash"
In [85]:
print(e1.first)
print(e1.fullname)
print(e1.last)
In [86]:
del e1.fullname
In [87]:
print(e1.first)
print(e1.fullname)
print(e1.last)
No comments :
Post a Comment