[Python] Why are "self" and "cls" interchangeable as the first argument in class and instance...

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I understand from the question "Difference between 'cls' and 'self' in Python classes?" that cls is used in class methods, whereas self is used in instance methods.

    However, I used self in class methods and it is still working fine.

    For example,

    class Test:

    @classmethod
    def hello(self, name):
    print ('hello '+name)

    Test.hello('Tom')


    What, exactly, does cls or self do in class methods?

    Continue reading...

Compartilhe esta Página