Search code examples
pythonlistclassmethodscall

Calling a function inside a class to set the element of list


I'm trying to implement my first class, partly as a way to break up the modeling and solving of a math problem I'm having trouble with. I don't think my problem relates to the class, but...?

The error keeps telling me that: "NameError: global name 'corner2' is not defined"

I tried moving the function call, but it still doesn't recognize it, so I put it back into the list declaration in my init function.

Here is my code:

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)? """

Solution

    • You have to use self as the first parameter of your class methods
    • When calling class methods inside your class you have to pass self as their first parameter(so that the class knows that you are talking about the current object)
    • You have to indent when defining a method inside your class

      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)]