简体   繁体   English

我在脚本中先前定义的函数的名称错误

[英]Getting Name Error for a function I've defined earlier in the script

I'm building a Rectangle Class to see if two rectangles are touching at the corners. 我正在建立一个Rectangle类,以查看两个矩形是否在拐角处接触。 It's the last exercise by the openbookproject for python chapter 16. http://openbookproject.net/thinkcs/python/english3e/classes_and_objects_II.html 这是最后一个练习由openbookproject为蟒蛇第16章http://openbookproject.net/thinkcs/python/english3e/classes_and_objects_II.html

The problem I'm having is that I've defined a function same_coordinates and then used that function to define a method corner_touching however when I do this I get a NameError: name 'same_coordinates' is not defined and I'm not sure why. 我遇到的问题是我已经定义了一个函数same_coordinates,然后使用该函数定义了corner_touching方法,但是当我这样做时,我得到了NameError:名称'same_coordinates'未定义,我不确定为什么。

class Rectangle:
"A class to manufacture Rectangle objects"
...
   def same_coordinates(P1,P2):
       return P1.x == P2.x and P1.y == P2.y

   def corner_touching(self,r2):
       r1bl = cao.Point(self.corner.x,self.corner.y)
       r1br = cao.Point(self.corner.x+self.width,self.corner.y)
       r1tr = cao.Point(self.corner.x+self.width,self.corner.y + 
       self.height)
       r1tl = cao.Point(self.corner.x,self.corner.y + self.height)

       r2bl = cao.Point(r2.corner.x,r2.corner.y)
       r2br = cao.Point(r2.corner.x+r2.width,r2.corner.y)
       r2tr = cao.Point(r2.corner.x+r2.width,r2.corner.y + r2.height)
       r2tl = cao.Point(r2.corner.x,r2.corner.y + r2.height)

    return same_coordinates(r1bl,r2tr) or same_coordinates(r1tl,r2br) or \
         same_coordinates(r1tr,r2bl) or same_coordinates(r1br,r2tl)

I've use "..." to represent the init and other methods which work fine. 我使用“ ...”来表示init和其他可以正常工作的方法。 The error I get when I use two rectangle is: 使用两个矩形时出现的错误是:

Exception has occurred: NameError name 'same_coordinates' is not defined 发生异常:NameError名称'same_coordinates'未定义

which is funny because I swear I've defined it two lines above corner_touching. 这很有趣,因为我发誓我已经在corner_touching上方两行定义了它。 Any help would be greatly appreciated!!! 任何帮助将不胜感激!!!

If intersection for method same_coordinates is like above then this is a class method, and you should call it on an object. 如果方法same_coordinates交集如上所示,则这是一个类方法,应该在对象上调用它。 If you move this method outside of class it should work. 如果将此方法移出类,则应该可以使用。

What you can try is doing this: 您可以尝试执行以下操作:

return self.same_coordinates(r1bl,r2tr) or self.same_coordinates(r1tl,r2br) or \
     self.same_coordinates(r1tr,r2bl) or self.same_coordinates(r1br,r2tl)

The thing is that you have self in the function. 问题是您在功能中具有自我。 In the class use self.FUNC to call it. 在该类中,使用self.FUNC进行调用。 I hope it works! 我希望它能起作用!

same_coordinates is an attribute of the Rectangle class, not a variable in any local or global scope. same_coordinatesRectangle类的属性,而不是任何局部或全局范围内的变量。 You need to access it via Rectangle or one of its instances. 您需要通过Rectangle或其实例之一来访问它。

def corner_touching(self,r2):
    # ...

    return self.same_coordinates(r1bl, r2tr) or \
           self.same_coordinates(r1tl, r2br) or \
           self.same_coordinates(r1tr, r2bl) or \
           self.same_coordinates(r1br, r2tl)

Since it is an attribute, it should also be declared as a static method, since it takes two Point instances--but no Rectangle instance--as arguments: 由于它一个属性,因此也应将其声明为静态方法,因为它将两个Point实例(但没有Rectangle实例)作为参数:

@staticmethod
def same_coordinates(P1,P2):
    return P1.x == P2.x and P1.y == P2.y

Or, you can define it as a global function, in which case you won't need to change corner_touching : 或者,您可以将其定义为全局函数,在这种情况下,您无需更改corner_touching

def same_coordinates(P1, P2):
    return P1.x == P2.x and P1.y == P2.y


class Rectangle:
    "A class to manufacture Rectangle objects"

   def corner_touching(self,r2):
       # ...

       return same_coordinates(r1bl, r2tr) or \
              same_coordinates(r1tl, r2br) or \
              same_coordinates(r1tr, r2bl) or \
              same_coordinates(r1br, r2tl)

Use __eq__ magic methods. 使用__eq__魔术方法。 What you are trying to do is exactly what __eq__ does. 您试图做的正是__eq__所做的。

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

p1 = Point(3, 4)
p2 = Point(4, 4)
p3 = Point(3, 4)
print(p1 == p2) # will print False
print(p1 == p3) # will print True

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM