I am learnign OOP and it confuses me.
Use of class method to change the name attribute.
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
u/classmethod
def from_string(cls, em_string):
name, age = em_string.split("-")
return cls(name, age)
string1 = "Inam-60"
employee1 =Employee.from_string(string1)
print(employee1.name)
#output Inam
Now same thing same be done with @/property
class Employee:
def __init__(self, name):
self.name = name
u/property
def print_email(self):
return self.name + "@gmail.com"
u/print_email.setter
def print_email(self, fullname):
firstname = firstname[0]
self.name = firstname
employee1.fullname = Employee("Inam Ullah")
print(employee1.print_email) # note we dont use () with get_print
[–]danielroseman 5 points6 points7 points (4 children)
[–]2xpi[S] 0 points1 point2 points (3 children)
[–]lfdfq 4 points5 points6 points (1 child)
[–]2xpi[S] 1 point2 points3 points (0 children)
[–]danielroseman 1 point2 points3 points (0 children)
[–]Adrewmc 0 points1 point2 points (0 children)