简体   繁体   English

现在需要从另一个文件中调用类中的方法时,如何重写该函数?

[英]How do I rewrite this function now that it needs to call upon a method in a class from a different file?

Here is the function (it's in a file, "worldmodel.py"): 这是函数(在文件“ worldmodel.py”中):

def add_entity(world, entity):
   pt = entities.get_position(entity)
   if within_bounds(world, pt):
      old_entity = occ_grid.get_cell(world.occupancy, pt)
      if old_entity != None:
         entities.clear_pending_actions(old_entity)
      occ_grid.set_cell(world.occupancy, pt, entity)
      world.entities.append(entity)

And here is the class in a file named, "occ_grid.py": 这是名为“ occ_grid.py”的文件中的类:

class Grid:
   def __init__(self, width, height, occupancy_value):
      self.width = width
      self.height = height
      self.cells = []

      # initialize grid to all specified occupancy value
      for row in range(0, self.height):
         self.cells.append([])
         for col in range(0, self.width):
            self.cells[row].append(occupancy_value)

   def set_cell(self, point, value):
      self.cells[point.y][point.x] = value

My question is, how would I rewrite the line of code in "def add_entity" that refers to "set_cell"? 我的问题是,我该如何重写“ def add_entity”中引用“ set_cell”的代码行? (Now that I've made set_cell a method of the class Grid) NOTE: Before I made set_cell part of the grid class, it was a function outside of the class (but still in the same file as the class) Thanks! (现在,我已将set_cell设置为Grid类的方法)注:在将set_cell设置为Grid类的一部分之前,它是该类之外的一个函数(但仍与该类位于同一文件中)谢谢!

You'll need to import occ_grid in your worldmodel.py , then instantiate a Grid object and call that objects set_cell() -method. 你需要import occ_gridworldmodel.py ,然后实例化一个Grid对象,并调用对象set_cell() -方法。 The add_entity needs to get the Grid -object as its parameter unless it can safely instantiate new ones at will. add_entity需要获取Grid作为其参数,除非它可以随意安全地实例化新对象。

Here's a naive example which does not work but demonstrates what I mean: 这是一个天真的示例,该示例不起作用,但演示了我的意思:

import occ_grid

g = occ_grid.Grid(your_width, your_height, occupancy)

def add_entity(world, entity, grid):
    pt = entities.get_position(entity)
    if within_bounds(world, pt):
        old_entity = grid.get_cell(world.occupancy, pt)
        if old_entity != None:
            entities.clear_pending_actions(old_entity)
        grid.set_cell(world.occupancy, pt, entity)
        world.entities.append(entity)

add_entity(world, entity, g)

There are two issues here, (1) calling functions across modules and (2) calling methods of classes. 这里有两个问题,(1)跨模块调用函数,(2)调用类的方法。 It seems you can already do (1). 看来您已经可以做到(1)。

The trick is that although methods are defined as 诀窍在于,尽管方法定义为

 def methodName(self, ...)

They are called as 他们被称为

 object.methodName(...)

And object implicitly becomes the "self" Parameter. 对象隐式地成为“自我”参数。 Here is an example: 这是一个例子:

import occ_grid  # Import the module (file) that contains Grid.
   .
   .   
world.occupancy = occ_grid.Grid()  # Create an instance of Grid.
   .
   .
def add_entity(world, entity):
   pt = entities.get_position(entity)       
   .
   .   
   world.occupancy.set_cell(pt, entity)

In this example, grid is a global variable, which is probably not a good design. 在此示例中, grid是全局变量,可能不是一个好的设计。 I guess it should be a property of world , but that's only a guess. 我想这应该是world的财产,但这只是一个猜测。

Unless you make the set_cell function a static method of the Grid class, you're going to need and instance of Grid . 除非使set_cell函数成为Grid类的静态方法,否则将需要Grid 实例

from occ_grid import Grid

I am going to make an assumption here, and say that your want your grid to be part of the world? 我将在此处进行假设,并说您希望网格成为世界的一部分? Either way, this is an example of instantiating that class. 无论哪种方式,这都是实例化该类的示例。

class World:
    grid = Grid()

    def add_entity(world, entity):
        # All that other stuff.
        world.grid.set_cell(pt, entity)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果函数(在类之外)需要使用类中的方法,我该如何调用该方法? - If a function (that's outside of the class) needs to use a method from a class, how do I call the method? 如何从不同文件中的不同类调用方法? - How do I call on a method from a different class in a different file? 如何调用在其他函数中定义的Tkinter标签? - How do I call upon Tkinter labels defined in a different function? 如何从 Python 中的另一个文件调用类方法? - How do I call a class method from another file in Python? 如何让每个子类的一个类方法不同(并从基类调用它) - How do I get one class method to be different for each child (and call it from base class) 如何在Python中从其他类调用类方法? - How Can I Call a Class Method From a Different Class in Python? 如何使用 Kivy 语言在不同的 class 中调用 function? - How do I call a function in a different class in the Kivy Language? 如何调用需要带有“引号”的字符串的函数? - How do I call a function that needs strings with “quotes”? 如何从不同的脚本调用方法? (Python) - How do I call a method from a different script? (Python) 如何调用 function 中的某个参数? - How do I call upon a certain argument within a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM