Setters and property decorators in python
class Employee:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
# self.email = f"{fname}{lname}@gmail.com"
def explain(self):
return f"This employee is {self.fname} {self.lname} and the email is {self.email}"
@property # Decorator....
def email(self):
if self.fname == None or self.lname == None:
return "Email not set please set email with setter"
return f"{self.fname}{self.lname}@gmail.com"
@email.setter
def email(self, string):
print("Setting now...")
split = string.split("@")[0]
a = split.split(".")
self.lname = a[1]
self.fname = a[0]
@email.deleter
def email(self):
self.fname = None
self.lname = None
umair = Employee("numan", "Supporter")
print(umair.email)
umair.email = "umair.jutt@gmail.com"
print(umair.explain())
# print(pakistani_supporter.email)
del umair.email
print(umair.email)
umair.email = "Rehman.imran@gmail.com"
print(umair.email)
Comments
Post a Comment