0

I have 3 classes :

class Scene(object):
    def enter(self):
        pass

class CentralCorridor(Scene):
    def enter(self):
        pass

class Map(object):
    def __init__(self, start_game): 
        pass

And the class map is initiated like this :

a_map = map('central_corridor')

It means that there is a map(obviously not graphical like a maze let's suppose) in which the first scene of the game(the game is like zork) is central corridor.

So I want to use the enter method of the class CentralCorridor in the map class but I am confused by the fact that the class CnetralCorridor itself inherits from the class Scene. I don't know how can I use the delegation method as explained here : https://stackoverflow.com/a/2797332/2572773

Community
  • 1
  • 1
kartikeykant18
  • 1,737
  • 6
  • 28
  • 42

3 Answers3

1

1) It's a good practice for Python classes to start with an uppercase letter. Furthermore, the name map is a built-in python function.

2) what's wrong with passing a Scene instance on your map class?

class Map(object):
    def __init__(self, scene):
        self.scene = scene
    def enter(self):
        self.scene.enter()

a_map = Map(CentralCorridor())
rantanplan
  • 7,283
  • 1
  • 24
  • 45
0

Would this code help:

class Scene(object):
    def enter(self):
        print 'Scene Object'

class CentralCorridor(Scene):
    def enter(self):
        print 'CentralCorridor object'

class Map(object):
    def __init__(self, start_game):
        self.start_game = start_game
        if self.start_game == 'central_corridor':
            whatever = CentralCorridor().enter()

a_map = Map('central_corridor')

You should not use map, but Map instead, because map() is a build-in function

Ivaylo
  • 2,082
  • 1
  • 13
  • 13
  • I don't understand why you have to do a string comparison in order to instantiate the proper object(in this case CentralCorridor). Unless you're trying to be as close to what the OP asked, which is quite broken anyway. – rantanplan Apr 23 '14 at 13:44
  • Well, yes I am trying to be close. Since I have no idea, why you call an object with a string and ask for a method. I guess depending on the string you will choose a method to call. That is why. – Ivaylo Apr 23 '14 at 13:49
  • OK you were trying to be close to the original question. Although I didn't get the rest of what you said. You don't choose a *method* depending on the string. You choose a *Class*. The method is always *enter*, as far as I can see. That's why I don't get the string comparison. – rantanplan Apr 23 '14 at 13:53
  • Yes, you choose a class, my bad. – Ivaylo Apr 23 '14 at 14:43
-1

First, you should rename your map class, since map is a builtin function you'll shadow here.

To answer your question: you can call CentralCorridor.enter(self) to explicitly call CentralCorridor's enter method on the current instance (which does not have to be a CentralCorridor instance).

ch3ka
  • 11,792
  • 4
  • 31
  • 28
  • so i can directly right CentralCorridor.enter and set it to a variable in Map class?? – kartikeykant18 Apr 23 '14 at 13:25
  • what you suggest seems like a really bad idea. But maybe I am misunderstanding something. Could you provide an actual example? Because this is more of a comment than an answer, as it currently stands. – rantanplan Apr 23 '14 at 13:33