Skip to content Skip to sidebar Skip to footer

How To Use A Method In A Class From Another Class That Inherits From Yet Another Class Python

I have 3 classes : class Scene(object): def enter(self): pass class CentralCorridor(Scene): def enter(self): pass class Map(object): def __init__(self

Solution 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?

classMap(object):def__init__(self, scene):
        self.scene = scene
    defenter(self):
        self.scene.enter()

a_map = Map(CentralCorridor())

Solution 2:

Would this code help:

classScene(object):defenter(self):
        print 'Scene Object'classCentralCorridor(Scene):defenter(self):
        print 'CentralCorridor object'classMap(object):def__init__(self, start_game):
        self.start_game = start_game
        ifself.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

Solution 3:

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).

Post a Comment for "How To Use A Method In A Class From Another Class That Inherits From Yet Another Class Python"