1
\$\begingroup\$

I have this class for my game that is a scene manager. I followed a tutorial and I am confused on why the self.scene.manager is equal to self

class SceneManager(object):
    def __init__(self):
        self.go_to(TitleScene())

    def go_to(self, scene):
        self.scene = scene
        self.scene.manager = self #<--

In the main game scene I also have a similar thing happening

class GameScene(Scene):
    def __init__(self, levelno):
        super(GameScene, self).__init__()
        self.player = Player(5, 40)
        self.player.scene = self

What is the purpose of setting it to self?

Another Question: I have been using classes and for most of them I create a class and I include this within the class

Class myClass():
   super(myClass, self).__init__() #<----- This line here

Does this have something to do with my original question? What is the purpose of doing this?

\$\endgroup\$
1
\$\begingroup\$

When you use something like self.foo.bar = self from the class bar, you're telling foo which bar it is linked to. Basically,

    self.scene = scene
    self.scene.manager = self

is used to set reference in both ways, so that from the SceneManager, you can know which scene is managed, and from the scene, you can get access to the scene manager through the manager variable.

Does this have something to do with my original question? What is the purpose of doing this?

No. Presumably, your current code looks more like this:

class myBaseClass:
    def __init__(self):
        # initialize stuff

class myClass(myBaseClass):
    def __init__(self):
        super(myClass, self).__init__()
        # initialize more stuff

According to the documentation, what you're actually doing here is calling the __init__() method of a parent class; i.e. you're calling the constructor of the parent. You do it like that because since you define the function in both classes, the one defined in the child class (myClass) overrides the one from the parent class (myBaseClass), i.e. it hides it. To "force" the usage of the parent's class, using super like that seems the way to go.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.