Python __init_;()正好接受4个参数(给定3个)

Python __init_;()正好接受4个参数(给定3个),python,Python,点通过给出x和y坐标来定义。两个点之间的距离可以使用距离法计算。点应至少具有以下特性x、y和距离_,可公开使用 圆是通过给定圆心和半径来定义的。圆有一种方法,即“是否在圆内”,用于回答给定点是否在圆内。圆应至少具有以下特性:圆心、半径和内部 这里的问题是我不能初始化我的父类和基类 旁注:功能和课程距离,圆圈,圆圈。在里面只需要一个p import math class Point(): def __init__(self, xpoint, ypoint): self.xp

点通过给出x和y坐标来定义。两个点之间的距离可以使用距离法计算。点应至少具有以下特性x、y和距离_,可公开使用

圆是通过给定圆心和半径来定义的。圆有一种方法,即“是否在圆内”,用于回答给定点是否在圆内。圆应至少具有以下特性:圆心、半径和内部

这里的问题是我不能初始化我的父类和基类

旁注:功能和课程距离,圆圈,圆圈。在里面只需要一个p

import math
class Point():
    def __init__(self, xpoint, ypoint):
        self.xpoint = xpoint
        self.ypoint = ypoint
    def distance_from(xpoint, ypoint):
        distance = math.sqrt(((p1.xpoint-p2.xpoint)**2)+((p1.ypoint-p2.ypoint)**2))
        print(distance)
class Circle(Point):
    def __init__(self, xpoint, ypoint, r):
       super(Circle, self).__init__(xpoint, ypoint)
       self.r = r
    def is_inside(xpoint, ypoint):        
        if self.r > distance:
            print("False")
        else:             
            print("True") 
p1 = Point(0, 0) 
p2 = Point(2, 4) 
p1.distance_from(p2)
circle  = Circle(p2,4)
circle.is_inside(p1)

手动调用
\uuuu init\uuuu
时,您必须提供
self
参数。

将您的coe更改为:我为您的错误添加了注释

import math
class Point(object): # Add object here
    def __init__(self, xpoint, ypoint):
        self.xpoint = xpoint
        self.ypoint = ypoint
    def distance_from(self, xpoint, ypoint):  # Missing self here
        distance = math.sqrt(((p1.xpoint-p2.xpoint)**2)+((p1.ypoint-p2.ypoint)**2))
        print(distance)

class Circle(Point):
    def __init__(self, xpoint, ypoint, r):
        super(Circle, self).__init__(xpoint, ypoint)
        self.r = r
    def is_inside(self, point):  # Missing self here
        if self.r > self.distance_from(point.xpoint, point.ypoint): # You'll have to callculate distance here
            print("False")
        else:             
            print("True") 
p1 = Point(0, 0) 
p2 = Point(2, 4) 
p1.distance_from(p1.xpoint, p1.ypoint)  # Missing points
c  = Circle(p1.xpoint, p1.ypoint, 4)  # Missing points
c.is_inside(p2)
如果不想使用super()。init请使用以下命令:

Point.__init__(self, xpoint, ypoint)  # Change super to the superclass name.  Add self to the init method
一些注意事项:

  • 成员函数始终将self作为第一个参数
  • 成员函数只能访问通过self或function参数访问的变量,而不能访问全局范围

    import math
    
    class Point(object):
        def __init__(self, xpoint, ypoint):
            self.xpoint = xpoint
            self.ypoint = ypoint
    
          # Here you where missing self and you were accessing p2 that exists in global scope.
        def distance_from(self, another_point):
              distance =  math.sqrt(((self.xpoint-another_point.xpoint)**2)+((self.ypoint-another_point.ypoint)**2))
              print(distance)
    
    
    class Circle(Point):
        def __init__(self, xpoint, ypoint, r):
          super(Circle, self).__init__(xpoint, ypoint)
          self.r = r
    
        def is_inside(self, another_point):
            # you forgot to calculate the distance
            if self.distance_from(another_point) < self.r:
                print("True")
            else:
                print("False")
    
    p1 = Point(0, 0)
    p2 = Point(2, 4)
    p1.distance_from(p2)
    
    #create a circle centered at 2,4 with radius 4
    circle  = Circle(2, 4, 4)
    circle.is_inside(p1) # returns "True"
    
    导入数学
    类点(对象):
    定义初始化(self、xpoint、ypoint):
    self.xpoint=xpoint
    self.ypoint=ypoint
    #在这里,您将看到缺少self的地方,您正在访问全局范围中存在的p2。
    def距离(自身,另一个点):
    距离=数学sqrt((self.xpoint-other_point.xpoint)**2)+(self.ypoint-other_point.ypoint)**2))
    打印(距离)
    类圆(点):
    定义初始化(self,xpoint,ypoint,r):
    超级(圆,自).\uuuu初始\uuuuuu(xpoint,ypoint)
    self.r=r
    def在内部(自身,另一个点):
    #你忘记计算距离了
    如果自距离(另一个点)

您到底有什么问题?你复制了作业的文本,这是我目前看到的。你的问题是什么?距离和内部的距离缺少开始时的自变量。Javier,我的距离函数实际上是workscircle=Circle(p2,4)必须更改为Circle=Circle(2,4,4)亲爱的Vincent,谢谢你的帮助。实际上,那些你评论说缺少p1或p2的,在参数中只取一个p,p1或p2,或者任何一个p,这就是你想要的吗?Btw距离未初始化。另外,如果您想使用super init,您需要在python 2.7中添加对象,我可以不使用super吗?是的,我会将其添加到我的应答回溯(最近一次调用):文件“point\u circle\u tests.py”,第4行,在from geometry import point,circle File“/tmp/grader/2014112508952410825/geometry.py”,第31行,在circle.is\u(p1)文件中“/tmp/grader/20141125082952410825/geometry.py”,第20行,如果self.distance离(另一个点)self已存在于方法中。该错误与其他参数有关。