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

后端 未结 5 1966
北海茫月
北海茫月 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:16

    It's used in case of a class method. Check this reference for further details.

    EDIT: As clarified by Adrien, it's a convention. You can actually use anything but cls and self are used (PEP8).

提交回复
热议问题