Naming the self parameter something else

后端 未结 4 1906
花落未央
花落未央 2021-01-13 17:27

In Python, this code is valid:

class A:

    def __init__(me):
        me.foo = 17

    def print_foo(myself):
        print(myself.foo)

    def set_foo(i,          


        
4条回答
  •  春和景丽
    2021-01-13 17:51

    The self parameter is really only named self by convention, and that isn't even a universally accepted convention - I also often see cls or this used instead.

    The term self isn't a keyword in python like it is in, say, Java. The user can choose to name it anything they want - although it would be better to choose one name and stick with that throughout the code for clarity, there isn't anything stopping you from naming it something different in every method.

提交回复
热议问题