Difference between 'cls' and 'self' in Python classes?

后端 未结 5 1962
北海茫月
北海茫月 2020-11-28 00:43

Why is cls sometimes used instead of self as an argument in Python classes?

For example:

class Person:
    def __init__(sel         


        
5条回答
  •  庸人自扰
    2020-11-28 01:06

    cls implies that method belongs to the class while self implies that the method is related to instance of the class,therefore member with cls is accessed by class name where as the one with self is accessed by instance of the class...it is the same concept as static member and non-static members in java if you are from java background.

提交回复
热议问题