python - (Python 2.7)使用访问器/更改器(mutator)从类访问变量

标签 python class typeerror accessor

(Python 2.7)我试图从 SierpinskiTriangle 类访问顶点变量并在列出的第二位代码中使用它,但它显示

TypeError: 'property' object is not iterable

我只能假设这是由于访问器/修改器造成的

基本代码:

class Fractal(object):

# the constructor
def __init__(self, dimensions):
    # the canvas dimensions
    self.dimensions = dimensions
    # the default number of points to plot is 50,000
    self.num_points = 50000
    # the default distance ratio is 0.5 (halfway)
    self.r = 0.5

# accessors and mutators
@property
def vertices(self):
    return self._vertices

@vertices.setter
def vertices(self, v):
    self._vertices = v


class SierpinskiTriangle(Fractal):
# the constructor
def __init__(self, canvas):
    # call the constructor in the superclass
    Fractal.__init__(self, canvas)
    # define the vertices based on the fractal size
    v1 = Point(self.dimensions["mid_x"], self.dimensions["min_y"])
    v2 = Point(self.dimensions["min_x"], self.dimensions["max_y"])
    v3 = Point(self.dimensions["max_x"], self.dimensions["max_y"])
    self.vertices = [ v1, v2, v3 ]

获取顶点的代码:

class ChaosGame(Canvas):

vertex_radius = 2
vertex_color = "red"
point_radius = 0
point_color = "black"

def __init__(self, master):
    Canvas.__init__(self, master, bg = "white")
    self.pack(fill = BOTH, expand = 1)

# a function that takes a string that represents the fractal to create
def make(self, f):
    if f == "SierpinskiTriangle":
        vertices = SierpinskiTriangle.vertices
    if f == "SierpinskiCarpet":
        vertices = []
    if f == "Pentagon":
        vertices = []
    if f == "Hexagon":
        vertices = []
    if f == "Octagon":
        vertices = []

    print vertices
    for point in vertices:
        self.plot_point(self, point, ChaosGame.vertex_color, ChaosGame.vertex_radius)

最佳答案

这是因为您正在访问类而不是该类型的对象。

让我们尝试一个最小的示例:

class Container:
    def __init__(self):
        self._content = range(10)

    @property
    def content(self):
        return self._content

    @content.setter
    def set_content(self, c):
        self._content = c

这有效:

c = Container()
for number in c.content:
    print(number)

(打印出 0 到 9 之间的数字)。

但这失败了:

for number in Container.content:
    print(number)

有错误

TypeError        Traceback (most recent call last)
<ipython-input-27-f1df89781355> in <module>()
      1 # This doesn't:
----> 2 for number in Container.content:
      3     print(number)

TypeError: 'property' object is not iterable

除了属性问题之外,您还没有初始化对象,因此__init__该类的函数从未被调用过并且 Container._content没有初始化。

事实上,如果你刚刚使用过,你也会遇到类似的问题

class Container:
    def __init__(self):
        self.container = range(10)

(只是在这种情况下它会是一个属性错误)。 最后一点:这个

for number in Container().content:  # note the '()'!!
    print(number)

又可以工作了,因为我们动态创建了一个容器对象。

关于python - (Python 2.7)使用访问器/更改器(mutator)从类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53113082/

相关文章:

jquery - 如何将一个类分配给 jquery 按钮 var

javascript - Uncaught ReferenceError : MasterSlider is not defined

python - Pytesseract 转换期间的 "ValueError: cannot filter palette images"

python - 在 TensorFlow 中平铺变量张量是否会创建新变量?

python - 如何使用 P4Python 创建编号更改列表?

python - DJango URL 反向错误 : argument to reversed() must be a sequence

JavaScript 无法识别函数

Python:如何递归地从嵌套数据结构(列表和字典)中删除无值?

Javascript 对象和 this 关键字

java - 使用类对象创建通用二维数组