Python 2.7 给定类的对象可调用一次,但不能再次调用

Python 2.7 给定类的对象可调用一次,但不能再次调用,python-2.7,matplotlib,Python 2.7,Matplotlib,我的代码在使用一个矩形时运行良好,但当我添加第二个矩形时,它会说“'point'对象不可调用”,尽管成功地为第一个矩形调用了它。我已经对矩形进行了足够多的不同测试,得出结论,唯一的原因是它现在尝试创建的不止一个矩形。有人能帮忙吗 下面是代码的开头,用于定义不同的元素及其参数 import matplotlib.pyplot as plt import numpy elementset = [] pointxs = [] pointys = [] class point(object):

我的代码在使用一个矩形时运行良好,但当我添加第二个矩形时,它会说“'point'对象不可调用”,尽管成功地为第一个矩形调用了它。我已经对矩形进行了足够多的不同测试,得出结论,唯一的原因是它现在尝试创建的不止一个矩形。有人能帮忙吗

下面是代码的开头,用于定义不同的元素及其参数

import matplotlib.pyplot as plt
import numpy

elementset = []
pointxs = []
pointys = []

class point(object):
    """General point in 2d space, with stored x value and y value.
    Created and used in elements to give them shape.
    """

    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)
        self.isAnchor = False

    def __repr__(self):
        return "(%d, %d)" % (self.x, self.y)

class element(object):
    """Most general class used to define any element to be used in
    the cross-section being worked with. Used as a basis for more
    specific classes. Has a coordinate value and a number of points that
    need to be generated for the general case.
    """

    def __init__(self, anchor_x, anchor_y):
        self.num_points = 0
        self.anchor_x = float(anchor_x)
        self.anchor_y = float(anchor_y)
        elementset.append(self)

    def getinfo(self):
        """Used for debugging, prints all general info for the element.
        Never called in the actual code."""
        print "Number of points: " + str(self.num_points)
        print "x coordinate: " + str(self.anchor_x)
        print "y coordinate: " + str(self.anchor_y)

    def debug(self):
        self.getinfo()

class rectangle(element):
    """A rectangle, assumed to be aligned such that all sides are
    either vertical or horizontal. Calls/assigns variables
    created in the element class via super().
    """

    def __init__(self, anchor_x, anchor_y, width, height):
        super(rectangle, self).__init__(anchor_x, anchor_y)
        self.title = "rectangle"
        self.num_points = 4
        self.width = float(width)
        self.height = float(height)
        self.generate()
        self.calculate()

    def generate(self):
        """Creates the points that frame the rectangle using coordinates.
        For a rectangle, the anchor point represents the bottom left point."""
        self.anchor = point(self.anchor_x, self.anchor_y)
        self.pointxpos = point(self.anchor_x + self.width, self.anchor_y)
        self.pointxypos = point(self.anchor_x + self.width, self.anchor_y + self.height)
        self.pointypos = point(self.anchor_x, self.anchor_y + self.height)
        self.points = [self.anchor, self.pointxpos, self.pointxypos, self.pointypos]
        self.plotpoints = [self.anchor, self.pointxpos, self.pointxypos, self.pointypos, self.anchor]
下面是调用这些函数的函数(仅定义了一个矩形):

这是成功的,绘制一个左下角为(0,0)的50x20矩形

但如果我在ar下面添加另一个元素:

ar = rectangle(0,0,50,20)
br = rectangle(50,20,10,10)
它抛出“'point'对象不可调用”。
老实说,我被这件事难住了,所以提前非常感谢您提供的帮助。

我想知道为什么这件事在第一个矩形中都能成功。原因可能是你没有表现出真实的态度

在任何情况下,问题都很简单:不要对类使用与(循环)变量相同的名称。

即以下工程:

ar = rectangle(0,0,50,20)
br = rectangle(23,16,22,13)

elementset.append(ar)
elementset.append(br)

for elefant in elementset:
    if isinstance(elefant,rectangle):
        elefant.generate()
        for wombat in elefant.plotpoints:
            pointxs.append(wombat.x)
            pointys.append(wombat.y)
        plt.plot(pointxs,pointys, linewidth=3)
…除了所有对象都将由一条线连接这一事实之外,这可能是另一个问题

ar = rectangle(0,0,50,20)
br = rectangle(23,16,22,13)

elementset.append(ar)
elementset.append(br)

for elefant in elementset:
    if isinstance(elefant,rectangle):
        elefant.generate()
        for wombat in elefant.plotpoints:
            pointxs.append(wombat.x)
            pointys.append(wombat.y)
        plt.plot(pointxs,pointys, linewidth=3)