python类中使用的'cls'变量是什么? [英] What is the 'cls' variable used in python classes?
本文介绍了python类中使用的'cls'变量是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么使用cls而不是self?
任何帮助感谢
Why is 'cls' used instead of 'self'? Any help appreciated
推荐答案
self
和cls
定义在 PEP 8
。正如阿德里安说的,这不是强制性的。这是一种编码风格。 PEP 8
说:
:
Function and method arguments:
始终使用 self
作为实例方法的第一个参数。
Always use self
for the first argument to instance methods.
对类方法的第一个参数始终使用 cls
。
Always use cls
for the first argument to class methods.
这篇关于python类中使用的'cls'变量是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文