Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

python - Why do I have pass an argument on every method call?

I don't have to pass self but I have to pass master in line 4 of the Code.

import game
window = game.Window()
cluster = game.Cluster(window)
cluster.circle(**window**,10,10,5)


. . .
class Cluster(Window):
    def __init__(self,master):
        self.obj = []
        self.x = []
        self.y = []
        self.hitbox = master.canvas.create_rectangle(0,0,0,0)
    def update(self,master):
        master.canvas.coords(self.hitbox,min(self.x)-2,min(self.y)-2,max(self.x)+2,max(self.y)+2)
    def circle(self,master,x,y,r):
        self.obj += [master.canvas.create_oval(x-r,y-r,x+r,y+r)]
        self.x += [x-r,x+r]
        self.y += [y-r,y+r]
        self.update(master)
question from:https://stackoverflow.com/questions/65921369/why-do-i-have-pass-an-argument-on-every-method-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your code to store the value of master as an instance attribute:

class Cluster(Window):
    def __init__(self,master):
        self.obj = []
        self.x = []
        self.y = []
        self.hitbox = master.canvas.create_rectangle(0,0,0,0)
        self.master = master

    def update(self):
        self.master.canvas.coords(self.hitbox,min(self.x)-2,min(self.y)-2,max(self.x)+2,max(self.y)+2)

    def circle(self,x,y,r):
        self.obj += [self.master.canvas.create_oval(x-r,y-r,x+r,y+r)]
        self.x += [x-r,x+r]
        self.y += [y-r,y+r]
        self.update()

So you don't have to pass it along to each method.

self gets a special treatment in Python and it's passed implicitly (https://docs.python.org/3/tutorial/classes.html#classes):

In C++ terminology, normally class members (including the data members) are public (except see below Private Variables), and all member functions are virtual. As in Modula-3, there are no shorthands for referencing the object’s members from its methods: the method function is declared with an explicit first argument representing the object, which is provided implicitly by the call. As in Smalltalk, classes themselves are objects. This provides semantics for importing and renaming. Unlike C++ and Modula-3, built-in types can be used as base classes for extension by the user. Also, like in C++, most built-in operators with special syntax (arithmetic operators, subscripting etc.) can be redefined for class instances.

Even more strictly speaking: the first argument - technically you can name it whatever you want (https://docs.python.org/3/tutorial/classes.html#random-remarks):

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...