Why is cls
sometimes used instead of self
as an argument in Python classes?
For example:
class Person:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
@classmethod
def from_fullname(cls, fullname):
cls.firstname, cls.lastname = fullname.split(' ', 1)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…