Python 在类内调用函数以设置list的元素

Python 在类内调用函数以设置list的元素,python,list,class,methods,call,Python,List,Class,Methods,Call,我正在尝试实现我的第一堂课,部分原因是为了打破一个数学问题的建模和解决。我认为我的问题与班级无关,但是 错误一直告诉我:“NameError:未定义全局名称‘corner2’” 我尝试移动函数调用,但它仍然无法识别它,因此我将它放回我的init函数的列表声明中 这是我的密码: class RotatedRectangle(object): def corner1(a,b): a/=2 b/=2 x=(a-b)*math.sin(math.pi/4) y=(a+b

我正在尝试实现我的第一堂课,部分原因是为了打破一个数学问题的建模和解决。我认为我的问题与班级无关,但是

错误一直告诉我:“NameError:未定义全局名称‘corner2’”

我尝试移动函数调用,但它仍然无法识别它,因此我将它放回我的init函数的列表声明中

这是我的密码:

class RotatedRectangle(object):

def corner1(a,b):
    a/=2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner2(a,b):
    a/=-2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner3(a,b):
    a/=-2
    b/=-2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def corner4(a,b):
    a/=2
    b/=2
    x=(a-b)*math.sin(math.pi/4)
    y=(a+b)*math.sin(math.pi/4)
    return (x,y)

def __init__(self, a, b,):
        """Return a Rotated rectangle object whose name is a function of a and b."""
        self.name = str(a) + "," + str(b) + "-rectangle"
        self.corners = [corner1(a,b), corner2(a,b), corner3(a,b), corner4(a,b)]



"""A rectangle with sides equal to even integers a and b is drawn on the          Cartesian plane.Its center (the intersection point of its diagonals) coincides with the point (0, 0),but the sides of the rectangle are not parallel to the axes; instead, they are forming 45 degree angles with the axes.

How many points with integer coordinates are located inside the given   rectangle (including on its sides)? """

您有一些识别错误,并且忘记添加self参数,它应该是:

import math

class RotatedRectangle(object):
  def __init__(self, a, b,):
        """Return a Rotated rectangle object whose name is a function of a and b."""
        self.name = str(a) + "," + str(b) + "-rectangle"
        self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]

  def corner1(self,a,b):
      a/=2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner2(self,a,b):
      a/=-2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner3(self,a,b):
      a/=-2
      b/=-2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

  def corner4(self,a,b):
      a/=2
      b/=2
      x=(a-b)*math.sin(math.pi/4)
      y=(a+b)*math.sin(math.pi/4)
      return (x,y)

我强烈建议您阅读此问题和所有答案

在Python中定义类的方法时,第一个参数通常设置为“self”。然后,在调用该方法时,在其前面加上
self.

以下是工作代码:

import math

class RotatedRectangle(object):

    def corner1(self,a,b):
        a/=2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner2(self,a,b):
        a/=-2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner3(self,a,b):
        a/=-2
        b/=-2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def corner4(self,a,b):
        a/=2
        b/=2
        x=(a-b)*math.sin(math.pi/4)
        y=(a+b)*math.sin(math.pi/4)
        return (x,y)

    def __init__(self, a, b,):
            """Return a Rotated rectangle object whose name is a function of a and b."""
            self.name = str(a) + "," + str(b) + "-rectangle"
            self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]
  • 必须使用self作为类方法的第一个参数
  • 在类内调用类方法时,必须将self作为其第一个参数传递(以便类知道您在谈论当前对象)
  • 在类中定义方法时必须缩进

    import math
    class RotatedRectangle(object):
        def corner1(self,a,b):
            a/=2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner2(self,a,b):
            a/=-2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner3(self,a,b):
            a/=-2
            b/=-2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def corner4(self,a,b):
            a/=2
            b/=2
            x=(a-b)*math.sin(math.pi/4)
            y=(a+b)*math.sin(math.pi/4)
            return (x,y)
    
        def __init__(self, a, b,):
            """Return a Rotated rectangle object whose name is a function of a and b."""
            self.name = str(a) + "," + str(b) + "-rectangle"
            self.corners = [self.corner1(a,b), self.corner2(a,b), self.corner3(a,b), self.corner4(a,b)]
    

我认为这正是问题所在,还有一点:)是的,有一点我不喜欢python。复制和粘贴似乎总是导致缩进错误。谢谢你的回复。我将接受你建议的阅读,似乎这门课上有些东西我还不理解或不太熟悉。@Carl,你必须一步一步地做。不要忘记将问题标记为已接受,如果有用,请进行投票:)