Why do pythonistas call the current reference “self” and not “this”?

后端 未结 8 1678
终归单人心
终归单人心 2020-12-13 12:13

Python is the language I know the most, and strangely I still don\'t know why I\'m typing \"self\" and not \"this\" like in Java or PHP.

I know that Python is older

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 12:53

    Check the history of Python for user defined classes:

    Instead, one simply defines a function whose first argument corresponds to the instance, which by convention is named "self." For example:

    def spam(self,y):
        print self.x, y
    

    This approach resembles something I had seen in Modula-3, which had already provided me with the syntax for import and exception handling.

    It's a choice as good as any other. You might ask why C++, Java, and C# chose "this" just as easily.

提交回复
热议问题