理解 super() 以及有关类构造函数的问题

发布于 2024-11-17 22:03:30 字数 913 浏览 1 评论 0原文

以下是我所引用的程序中的一段示例代码。我意识到我并没有真正的这个概念,所以我在这里问。

class Chef(games.Sprite):
    """
    A chef which moves left and right, dropping pizzas.
    """
    image = games.load_image("chef.bmp")

    def __init__(self, y = 0, speed = 2, odds_change = 200):
        """ Initialize the Chef object. """
        super(Chef, self).__init__(image = Chef.image,
                                   x = games.screen.width / 2,
                                   y = 55,
                                   dx = speed)

        self.odds_change = odds_change
        self.time_til_drop = 0

我的想法是这样的:

第六行 def __init__(self, y = 0, speed = 2, odds_change = 200) 给出了对象的初始值和内核中包含的值:

super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = 55,dx = speed) 处理对象的这些值初始化之后。使第六行值相当任意。例如,我能够将第六行构造函数中 y 的值更改为任意数字,并且该对象在屏幕上保持相同的 y 坐标。我的这个理解正确吗?

The following is a sample piece of code from a program that I'm referring to. I realized that I hadn't truly this concept, so I'm asking here.

class Chef(games.Sprite):
    """
    A chef which moves left and right, dropping pizzas.
    """
    image = games.load_image("chef.bmp")

    def __init__(self, y = 0, speed = 2, odds_change = 200):
        """ Initialize the Chef object. """
        super(Chef, self).__init__(image = Chef.image,
                                   x = games.screen.width / 2,
                                   y = 55,
                                   dx = speed)

        self.odds_change = odds_change
        self.time_til_drop = 0

This is what I thought is the case:

the sixth line def __init__(self, y = 0, speed = 2, odds_change = 200) gives the object its initial values and the values enclosed in the kernel:

super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = 55,dx = speed) deal with these values of the object after initialization. Making the sixth line values fairly arbitary. For example I was able to change the value of y in the sixth line constructor to any arbitary number, and the object stayed in the same y coordinate on the screen. Is my understanding of this correct?

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

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

发布评论

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

评论(2

春风十里 2024-11-24 22:03:30

不。 = 0= 2 等不会初始化对象 - 它们只是函数的默认参数。例如:

def foo(x, y=20):
    print x, y
foo(10, 30)
foo(10)

在您编写的代码中,这意味着您只需调用 Chef(),并在构造函数中调用变量 yspeed 的值code> 和 odds_change 将是默认值。

但这并没有设置实例变量。设置它们的代码位于 games.Sprite 的构造函数(Chef 的超类)中。

Naw. The = 0, = 2, etc., don't initialize the object - they're just default arguments to a function. Eg:

def foo(x, y=20):
    print x, y
foo(10, 30)
foo(10)

In the code you wrote, that means you can just call Chef(), and in the constructor, the values for the variables y, speed, and odds_change will be the default values.

This doesn't set the instance variables, though. The code that sets them is in games.Sprite's constructor (the superclass of Chef).

淡写薰衣草的香 2024-11-24 22:03:30

super实际上所做的就是调用父类相应的方法。 http://docs.python.org/library/functions.html#super
因此,在您的情况下,您正在从定义的类 Chef 调用类 games.Sprite__init__ 方法

您获得该对象的原因尽管您在第六行中更改了 y,但在同一位置,因为您没有更改传递给 super 函数的参数 y=55 。如果你将这个 y 更改为其他值,你的物体肯定会移动。

使用 super 时的一般做法是传递与从 __init__ 方法获取的参数相同的参数。在您的代码中,您没有将 __init__ 的任何参数传递给 super(Chef, self).__init__,因此未将 __init__ 中定义的参数传递给 super(Chef, self).__init__ > 或多或少没有意义。所以你的代码应该是这样的 -

class Chef(games.Sprite):
    """
    A chef which moves left and right, dropping pizzas.
    """
    image = games.load_image("chef.bmp")

    def __init__(self, x = games.screen.width / 2, y = 55, speed = 2, odds_change = 200):
        """ Initialize the Chef object. """
        super(Chef, self).__init__(image = Chef.image,
                               x = x,
                               y = y,
                               dx = speed)     # although this will work, you should either rename dx as speed or vice-versa

        self.odds_change = odds_change
        self.time_til_drop = 0

Rem,你可以在 __init__ 方法中获取更多参数,然后传递给 super(Chef, self).__init__ 函数。

What super actually does is calls the parent class' corresponding method. http://docs.python.org/library/functions.html#super
So, in your case, you are calling the __init__ method of the class games.Sprite from your defined class Chef

The reason you got the object at the same position although you changed y in the sixth line because you didn't change the parameter y=55 which you are passing to the super function. If you will change this y to something else, your object will definitely move.

The general practice while using super is to pass the same parameters as you get from the __init__ method. In your code, you are not passing any of the parameters of __init__ to super(Chef, self).__init__ and hence the parameters defined in __init__ are more or less meaningless. So your code should be some what like this -

class Chef(games.Sprite):
    """
    A chef which moves left and right, dropping pizzas.
    """
    image = games.load_image("chef.bmp")

    def __init__(self, x = games.screen.width / 2, y = 55, speed = 2, odds_change = 200):
        """ Initialize the Chef object. """
        super(Chef, self).__init__(image = Chef.image,
                               x = x,
                               y = y,
                               dx = speed)     # although this will work, you should either rename dx as speed or vice-versa

        self.odds_change = odds_change
        self.time_til_drop = 0

Rem, you can take more parameters in the __init__ method then you pass to super(Chef, self).__init__ function.

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