2012-05-31 11 views
8

Poniższy fragment kodu daje mi błąd z jakiegoś powodu, może ktoś mi powiedzieć co byłoby problemu ..Python klasa - Super zmienna

Zasadniczo utworzyć klasy 2 pkt & Circle..THe circle próbuje odziedziczyć klasę Point.

Code: 


class Point(): 

    x = 0.0 
    y = 0.0 

    def __init__(self, x, y): 
     self.x = x 
     self.y = y 
     print("Point constructor") 

    def ToString(self): 
     return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}" 

class Circle(Point): 
    radius = 0.0 

    def __init__(self, x, y, radius): 
     super(Point,self).__init__(x,y) 
     self.radius = radius 
     print("Circle constructor") 

    def ToString(self): 
     return super().ToString() + \ 
       ",{RADIUS=" + str(self.radius) + "}" 


if __name__=='__main__': 
     newpoint = Point(10,20) 
     newcircle = Circle(10,20,0) 

Błąd:

C:\Python27>python Point.py 
Point constructor 
Traceback (most recent call last): 
    File "Point.py", line 29, in <module> 
    newcircle = Circle(10,20,0) 
    File "Point.py", line 18, in __init__ 
    super().__init__(x,y) 
TypeError: super() takes at least 1 argument (0 given) 
+0

Czy wprowadzić pewne zmiany do źródła? Czy twoje wywołanie "init" wyglądało tak jak pierwotnie? –

+4

'ToString', oh! moje (pythonic) oczy krwawią. – juliomalegria

Odpowiedz

13

Wygląda na to, że już może być ustalona oryginalny błąd, który został spowodowany przez super().__init__(x,y) jak wskazuje komunikat o błędzie, chociaż twój dylemat był nieco niepoprawny, zamiast super(Point, self) od klasa Circle powinieneś użyć super(Circle, self).

Zauważ, że nie ma innego miejsca, które nazywa super() nieprawidłowo, wewnątrz Circle „s ToString() metody:

 return super().ToString() + \ 
       ",{RADIUS=" + str(self.radius) + "}" 

Dotyczy to kod na Python 3, ale na Pythonie 2 super() wymaga argumentów, przepisać to jako następujące:

 return super(Circle, self).ToString() + \ 
       ",{RADIUS=" + str(self.radius) + "}" 

Polecam również pozbycie się kontynuacji linii, zobacz Maximum Line Length section of PEP 8 dla zalecanego sposobu ustalania tego.

+0

Komunikat o błędzie jawnie stwierdza, że ​​błąd * nie * pochodzi z tej linii. –

+0

@MarkRansom - Racja! Dzięki. –

+0

powinieneś polecić, że 'ToString()' nie jest (w ogóle) prawidłowym zadaniem, do czego służy '__str__'. – juliomalegria

7

zajmuje tylko zajęcia w nowym stylu. Aby to naprawić, należy rozszerzyć klasę Point z object. Tak:

class Point(object): 

także prawidłowy sposób korzystania z super (..) jest tak:

super(Circle,self).__init__(x,y) 
0
class Point(object): 

x = 0.0 
y = 0.0 

def __init__(self, x, y): 
    self.x = x 
    self.y = y 
    print("Point constructor") 

def ToString(self): 
    return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}" 

class Circle(Point,object): 
radius = 0.0 

def __init__(self, x, y, radius): 
    super(Circle,self).__init__(x,y) 
    self.radius = radius 
    print("Circle constructor") 

def ToString(self): 
    return super(Circle, self).ToString() + \ 
      ",{RADIUS=" + str(self.radius) + "}" 


if __name__=='__main__':  
    newpoint = Point(10,20)  
    newcircle = Circle(10,20,0) 
Powiązane problemy