I have a question. I am watching class tutorials and cam across class methods. My question can be better understood after seeing the code. I will show the code first.
The Code:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.from_lastname()
def from_lastname(self):
Person.mostpreviouslastname = self.lastname
p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)
p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)
That was the part without class methods. This is with:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
Person.from_lastname(self.lastname)
@classmethod
def from_lastname(cls, last_name1):
cls.mostpreviouslastname = last_name1
p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)
p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)
They both work the same. So my question is why even use class methods at all?