【发布时间】:2018-01-03 07:34:26
【问题描述】:
我是编程新手,我对如何调用 Python 2 中的类中定义的方法/参数感到困惑。例如(障碍是以前的类),
class Block(Obstacle):
def __init__(self, origin, end, detection=9.):
self.type = 'block'
self.origin = origin
self.end = end
x1 = self.origin[0]
y1 = self.origin[1]
x2 = self.end[0]
y2 = self.end[1]
def __str__(self):
return "block obstacle"
当我生成环境时,我定义了不同的 x1、y1、x2 和 y2 值(本质上表示块角的坐标点)。我有另一种后来的方法,在计算某些东西时我需要 x1、y1、x2 和 y2 的值,但是我对如何将它们实际调用到这个新函数中感到困惑?我会在这个新函数中添加什么参数?
【问题讨论】:
-
如果您使用 self.name 定义它们,它们将成为类中的“全局”变量。你可以使用 self.x, self.y = self.origin
-
@AntonvBR:我会称它们为 instance 变量。全球的是另一回事......
-
@SergeBallesta 你说得对,我找不到更好的名字,因此引用“”标记。
标签: python python-2.7 methods parameters