[英]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_grid
在worldmodel.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.