Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何组织此代码?_Python_Class_Methods - Fatal编程技术网

Python 如何组织此代码?

Python 如何组织此代码?,python,class,methods,Python,Class,Methods,我试图通过将函数转换为方法,将函数分类为适当的类(如果合适的话,可以将函数留在类之外)。但我不确定如何处理这个特定的函数(如下所示) 首先,这是我的课堂: class OreBlobAction: def __init__(self, entity, image_store): self.entity = entity self.image_store = image_store def ore_blob_action(self, world, action

我试图通过将函数转换为方法,将函数分类为适当的类(如果合适的话,可以将函数留在类之外)。但我不确定如何处理这个特定的函数(如下所示)

首先,这是我的课堂:

class OreBlobAction:
   def __init__(self, entity, image_store):
      self.entity = entity
      self.image_store = image_store

   def ore_blob_action(self, world, action, ticks):
      entity = self.entity
      entity_pt = entities.get_position(self.entity)
      vein = find_nearest(world, entity_pt, entities.Vein)
      (tiles, found) = blob_to_vein(world, self.entity, vein)

      next_time = ticks + entities.get_rate(entity)
      if found:
         quake = create_quake(world, tiles[0], ticks, self.image_store)
         worldmodel.add_entity(world, quake)
         next_time = ticks + entities.get_rate(entity) * 2

      schedule_action(world, self.entity,
         OreBlobAction(self.entity, self.image_store), next_time)

      return tiles
现在,这里是函数:

def take_action(world, action, ticks):
   entities.remove_pending_action(action.entity, action)
   if isinstance(action, VeinAction):
      return vein_action(world, action, ticks)
   elif isinstance(action, MinerNotFullAction):
      return miner_not_full_action(world, action, ticks)
   elif isinstance(action, MinerFullAction):
      return miner_full_action(world, action, ticks)
   elif isinstance(action, OreBlobAction):
      return ore_blob_action(world, action, ticks)
   elif isinstance(action, OreTransformAction):
      return ore_transform_action(world, action, ticks)
   elif isinstance(action, EntityDeathAction):
      return entity_death_action(world, action, ticks)
   elif isinstance(action, WyvernSpawnAction):
      return wyvern_spawn_action(world, action, ticks)
   elif isinstance(action, WyvernAction):
      return wyvern_action(world, action, ticks)
   elif isinstance(action, VeinSpawnAction):
      return vein_spawn_action(world, action, ticks)
   elif isinstance(action, AnimationAction):
      return animation_action(world, action, ticks)

   return []
如您所见,此函数不仅考虑OreBlobAction类的操作,还考虑多个其他类的操作。将此函数保留在OreBlobAction类之外是否更好?还是有更好的方法

注意:如果我将此函数保留在OreBlobAction类之外,并尝试运行该程序,则会出现以下错误:

NameError: global name 'ore_blob_action' is not defined

关于动作类型的big switch语句是重构的一个危险信号。是否有任何东西可以阻止您将“takeaction”方法移动到action类本身中?比如说

class Action(object):
    """ An action that can be performed on the game world. """

    def perform(self, world, ticks):
        """ Perform the action. """
        raise NotImplementedError()
这将是您的基本操作类,然后在每种类型的操作中,您将覆盖
perform(…)
方法,例如

class WyvernSpawnAction(Action):
    """ An action that spawns a Wyvern. """

    [... Some action specific initialisation code here ...]

    def perform(self, world, ticks):
        """ Spawn a Wyvern. """
        world.spawn(Wyvern(...))

从世界上删除操作的样板将保留下来,现在您可以自由添加新类型的操作,而无需在函数中最终添加数百个比较。此外,您现在可以处理这样的情况:操作可以继承其他操作的行为,而无需非常小心比较的顺序。

该函数的作用是什么?为什么要使用所有这些单独的函数,而不是直接调用Action子类的方法?我们的任务是通过将函数转化为方法,将函数分类为适当的类。或者,如果更合适的话,您可以将函数留在类之外。我真的很困惑,我是应该把它包括在OreBlobAction类中,还是应该把它删掉(仅仅将它们转换为方法并不是重点。API应该是这样的;您应该能够采取任何类型的操作对象,调用名为的方法,例如,
Action
,并让它根据操作的类型执行适当的代码,而不必测试它是否是
VeinSpawnAction
并调用
vein\u spawn_动作
。是的,但是如果我像这样离开它,我会得到上面列出的错误。:(所以我不知道该怎么办。但是没有人说“像这样离开它”!你应该完全删除该函数,并将动作放入子类的方法中,就像Brett说的那样。