Python类中使用的’cls’变量是什么?
作者:互联网
为什么在Python类中有时使用cl而不是self作为参数?
例如:
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)
解决方法:
“自我”和“cls”之间的区别在PEP 8
中定义.正如阿德里安所说,这不是强制性的.这是一种编码风格. PEP 8说:
Function and method arguments:
Always use
self
for the first argument to instance methods.Always use
cls
for the first argument to class methods.
标签:self,python,class,object,terminology 来源: https://codeday.me/bug/20190918/1810466.html