Python 3.x 创建Python对象的最佳位置是哪里

Python 3.x 创建Python对象的最佳位置是哪里,python-3.x,Python 3.x,所以我正在用Python编写一个基于OOP的程序,我想问:在哪里创建对象最好?假设我的程序中有这个对象 class Player(Item): def __init__(self,Name,Diff,Money,Experience): self.name = Name self.diff = Diff self.money = Money self.experience = Experience 但我也有一个主要的班级游

所以我正在用Python编写一个基于OOP的程序,我想问:在哪里创建对象最好?假设我的程序中有这个对象

class Player(Item):
    def __init__(self,Name,Diff,Money,Experience):
        self.name = Name
        self.diff = Diff
        self.money = Money
        self.experience = Experience

但我也有一个主要的班级游戏。我应该在类游戏内部创建玩家对象,还是在所有类之外创建玩家对象,这样它才是全局的?我不知道应该在哪里制作它,以便在需要时随时可以访问它。

你说的是在哪里定义类;这是一个风格问题。让我们看一看政府对此有何评论:

在顶级函数和类定义周围有两个空行

好的。。。这没有太大帮助,因为现在我必须用自己的知识来回答这个问题。嗯

嗯,我想这取决于你想说什么。如果你有这个:

class Game:
    class Player:
        def __init__(self, name, difficulty, money=0, experience=0):
            self.name = name
            self.difficulty = difficulty
            self.money = money
            self.experience = experience
这就意味着,如果您更改
游戏
实现,您必须更改
玩家
实现,并且在不更改
游戏
实现的情况下,您无法更改
玩家
实现;i、 例如,它们是紧密耦合的。因此,代码可能如下所示:

        def attack(self, game, other):
            game.hurt(other, 1000 // self.difficulty)
            if other.hp == 0:
                self.money += other.money
                other.money = 0

    def hurt(self, enemy, damage):
        enemy.hp -= damage // enemy.difficulty
        if enemy.hp <= 0:
            enemy.hp = 0
            enemy.die(self)
            self.player.experience += enemy.difficulty
class Game:
    def __init__(self):
        self._entities = set()

    def register(self, entity):
        if entity not in self._entities:
            self._entities.add(entity)

    def unregister(self, entity):
        if entity in self._entities:
            self._entities.remove(entity)

    def end(self):
        print("Game over")
        raise SystemExit(0)

class Entity:
    def __init__(self, *args, game, **kwargs):
        super().__init__(*args, **kwargs)
        self.game = game
        game.register(self)

    def die(self):
        self.game.unregister(self)

class Fighter(Entity):
    def __init__(self, *args, difficulty, money=None, **kwargs):
        super().__init__(*args, **kwargs)
        if money is None:
            money = (difficulty ** 2 - difficulty) // 2 + 1
        self.difficulty = difficulty
        self.money = money

        self._hp = 1000

    def attack(self, other):
        money = other.hit(self, self.difficulty):
        self.money += money

    def hit(self, other, damage):
        self._hp -= damage
        if self._hp <= 0:
            money = self.money
            self.money = 0
            self.die()
            return money
        # Didn't kill me, so gets no money
        return 0

class Player(Fighter):
    def __init__(self, *args, difficulty, money=None, experience=0, **kwargs):
        if money is None:
            money = 1000 // difficulty  # You get less money the more skilled
                                        # you are.
        self.experience = experience
        super().__init__(*args, difficulty=difficulty, money=money, **kwargs)

    def die(self):
        self.game.end()

    def attack(self, other):
        # Every time you attack an enemy, you get XP
        self.experience += other.difficulty
        super().attack(other)
def攻击(自我、游戏、其他):
游戏。伤害(其他,1000/自我。困难)
如果other.hp==0:
自我金钱+=其他金钱
其他货币=0
def伤害(自身、敌人、伤害):
敌方.hp-=伤害//敌方.难度

如果敌方.hp取决于您希望为这个新类提供的范围,还取决于您将使用它的频率以及使用它的不同文件的数量。您所说的“每当我需要它”的确切含义是什么,您在哪里需要它们?您可以在
Game
对象的
Player
属性中实例化
Player
对象。这要看情况了。。