开发者

Python: Best way to call methods from another class?

I have the following code:

class Player:
    def __init__(self, username, trip, model):
        self.username = username
        self.trip = trip
        self.hp = 100


    #### For player moving location/room ####
    def Move(self, dest):
        if dest == self.loc:
            return True

        # Check destination room is accessible from current room
        for room in aGame['rooms']:
            if room['ref'] == self.loc:
                for acsroom in room['acs']:
                    if acsroom == dest:
                        self.loc = dest
                        return True
        return False

aGame is an array which is defined outside this class so this code doesn't work. Since there is likely to be many other functions within this class which will possibly use the aGame array, should i do this:

class Player:
    def __init__(self, username, trip, model, aGame):
        self.username = username
        self.trip = trip
        self.hp = 100
        self.aGame = aGame            

    #### For player moving location/room ####
    def Move(self, dest):
        if dest == self.loc:
            return True

        # Check destination room is accessible from current room
        for room in self.aGame['rooms']:
            if room['ref'] == self.loc:
                for acsroom in room['acs']:
                    if acsroom == dest:
                        self.loc = dest
                        return True
        return False

Or would it be better to do this:

class Player:
    def __init__(self, username, trip, model):
        self.username = username
        self.trip = trip
        self.hp = 100          

    #### For player moving location/room ####
    def Move(self, dest, aGame):
        if dest == self.loc:
            return True

        # Check destination room is accessible from current room
        for room in aGame['rooms']:
            if room['ref'] == self.loc:
                for acsroom in room['acs']:
                    if acsroom == dest:
                        self.loc = dest
                        return True
        return False

Or should i make aGame a global variable (if so, how, note that this class is in a different file)?

Since aGame is an array that gets used all over the place, it doesn't seem correct to have to make copies of it inside every class. I may have this wrong, i'm slowly learning OOP so thanks for any 开发者_开发知识库help.


In my opinion, the first option is right out as it uses globals for no good reason. So the choice is between the second and the third.

The deciding feature is if you are going to want to use the same Player instance for more than one aGame value. If there will only ever be one value, then I would either pass it to the constructor (your option 2) or use gnibbler's idea of making it a class variable. I would probably favor passing it to the constructor for ease of testing.

If you want the same Player instance to be usable with multiple aGame values, then option 3 is probably the cleanest way to achieve that.


Is aGame the same for every instance? Then you can make it a class attribute either like this

class Player:
    aGame={'rooms':...}
    ...

or

Class Player:
    ...

Player.aGame={'rooms':...}

Within the class, you can still access it via self.aGame


Only the first choice will work. In the second example, for room in self.aGame['rooms'] will produce an error because nowhere is aGame bound to self. It would work if it was for room in aGame['rooms'], but then you would have to unnecessarily pass aGame every time you call move().

You can also make it a global variable, but it's best just to have each Player hold an aGame instance. If you need to change aGame and have multiple players, you should define it as a global variable.

Also, just nitpicking, but aGame is not an array, it's a dictionary. The Python language doesn't even have arrays (though some extensions do).


I'd use a slight variation on a global:

# in game.py or whatever
class Game(object):
    instance = {} # this is your aGame array

# in player.py or whatever:
from game import Game

class Player(object):
    # ...
    def Move(self, dest):
        # ...
        for room in Game.instance['rooms']:
            # ...

Alternatively, you could make Game a proper class, assign Game.instance = Game(...) somewhere during your initialization phase, and get more of a real singleton pattern going.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜