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?
What
superactually does is calls the parent class’ corresponding method. http://docs.python.org/library/functions.html#superSo, in your case, you are calling the
__init__method of the classgames.Spritefrom your defined classChefThe 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=55which you are passing to thesuperfunction. If you will change this y to something else, your object will definitely move.The general practice while using
superis 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__tosuper(Chef, self).__init__and hence the parameters defined in__init__are more or less meaningless. So your code should be some what like this –Rem, you can take more parameters in the
__init__method then you pass tosuper(Chef, self).__init__function.