python 对象是否需要为其类提供相同数量的参数?

发布于 2024-11-05 02:45:23 字数 618 浏览 2 评论 0原文

所以我在 pygame 教程中遇到了这个类定义:

class GameObject:
        def _init_(self,image,height,speed):
                self.speed = speed
                self.image = image
                self.pos = image.get_squarerect().move(0,height)
        def move(self):
                self.pos = self.pos.move(0, self.speed)
                if self.pos.right > 600:
                        self.pos.left = 0

然后编码器执行此操作以用对象填充数组(?):

objects = []
for x in range(10):
    o=GameObject(player, x*40, x)
    objects.append(o)   

我的问题是为什么在实例化对象时只传递 3 个参数,但该类是为了接受其中4个吗?

So I came across this class definition in a pygame tutorial:

class GameObject:
        def _init_(self,image,height,speed):
                self.speed = speed
                self.image = image
                self.pos = image.get_squarerect().move(0,height)
        def move(self):
                self.pos = self.pos.move(0, self.speed)
                if self.pos.right > 600:
                        self.pos.left = 0

The coder then does this to fill an array(?) with objects:

objects = []
for x in range(10):
    o=GameObject(player, x*40, x)
    objects.append(o)   

My question is why is it that only 3 arguments are passed when instantiating the object, but the class was made to accept 4 of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦巷 2024-11-12 02:45:23

self 是隐式传递的,它是对对象当前实例化的引用。官方教程对此进行了讨论:http://docs。 python.org/release/2.7/tutorial/classes.html#random-remarks

self is passed implicitly, it's a reference to the current instantiation of the object. This is discussed in the official tutorial here: http://docs.python.org/release/2.7/tutorial/classes.html#random-remarks

葬﹪忆之殇 2024-11-12 02:45:23

self 对象是在每次方法调用时隐式提供的。

对于 __init__ 来说,它是一个全新的对象,Python 提供了对该全新对象的引用作为 self。

对于 __init__ 之外的方法调用,您实际上提供了 self 引用的对象,不是作为显式参数,而是在调用该方法的对象中提供(object.method(foo) 对应于,在对象类的定义中,“def method(self, foo)”)。

因此,在您的示例中, __init__ 接受 4 个参数(包括 self),但仅使用 3 个参数(不包括 self)进行调用。 “move”方法需要 1 个参数(自身),但会使用 0 个参数进行调用。

The self object is supplied implicitly on every method call.

For __init__, it's a brand new object and Python supplies a reference to that brand new object as self.

For method calls other than __init__, you actually supply the object which is referenced by self, not as an explicit argument but in the object on which you call the method (object.method(foo) corresponds to, inside the definition of object's class, "def method(self, foo)").

So in your example, __init__ takes 4 arguments (including self) but is called with only 3 (excluding self). The "move" method takes 1 argument (self) but would be called with 0 arguments.

家住魔仙堡 2024-11-12 02:45:23

Python 要求任何实例方法的第一个参数是您选择引用当前实例的任何名称。

在我看来,使用“self”这个名字是一个应该严格遵守的约定,但它只是一个约定。

如果您从未遇到过使用“self”或“this”的语言,这里有一个快速概述:

定义一个类意味着您在一个盒子(实际上是一个“命名空间”)内编写代码以供以后使用。 “稍后使用”基本上意味着您将实例化该类,从而创建一个对象。虽然您很可能通过执行“myobj = myClass”来实例化对象,从而使对象的名称为“myobj”,但在您实际为类编写代码时无法知道其名称是什么。这就是为什么你使用“自我”。

Python requires that the first argument to any instance method be whatever name you choose to refer to the current instance.

Using the name 'self' is a convention that should, imo, be strictly adhered to, but it is just a convention.

In case you never came across a language that used 'self' or 'this', here's a quick rundown of what's going on:

Defining a class means you're writing code inside a box (a 'namespace' really) for later use. "Later use" basically means you'll instantiate the class, thereby creating an object. While you might well instantiate your object by doing "myobj = myClass", thereby making the name of your object "myobj", there's no way to know what its name will be at the time you actually write the code for the class. That's why you use 'self'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文