在 class 中调用一个函数来设置列表的元素
Calling a function inside a class to set the element of list
我正在尝试实现我的第一个 class,部分原因是为了打破建模和解决我遇到的数学问题。我认为我的问题与 class 无关,但是...?
错误一直告诉我:"NameError: global name 'corner2' is not defined"
我尝试移动函数调用,但它仍然无法识别它,所以我将它放回到我的 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)
我强烈建议你阅读这个问题和所有答案What is the purpose of self?
在为 Python 中的 class 定义方法时,第一个参数通常设置为 "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 作为 class 方法的第一个参数
- 在 class 中调用 class 方法时,您必须将 self 作为其第一个参数传递(以便 class 知道您正在谈论当前对象)
在 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)]
我正在尝试实现我的第一个 class,部分原因是为了打破建模和解决我遇到的数学问题。我认为我的问题与 class 无关,但是...?
错误一直告诉我:"NameError: global name 'corner2' is not defined"
我尝试移动函数调用,但它仍然无法识别它,所以我将它放回到我的 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)
我强烈建议你阅读这个问题和所有答案What is the purpose of self?
在为 Python 中的 class 定义方法时,第一个参数通常设置为 "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 作为 class 方法的第一个参数
- 在 class 中调用 class 方法时,您必须将 self 作为其第一个参数传递(以便 class 知道您正在谈论当前对象)
在 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)]