python - How to fix my math class? -
this code:
class math(): def __init__(self, x, y): self.x = x self.y = y class pythagorus(math): def __init__(self, x, y): math.__init__(self, x, y) def __str__(self): import math return math.sqrt(x**2+y**2) q = pythagorus(4, 5) print(q)
how make function out of class, if makes sense, want return result of math.sqrt(x*2+y*2), can't seem work? in advance!
you need refer self
access attributes on class:
class pythagoras(math): def __str__(self): import math return str(math.sqrt(self.x**2 + self.y**2))
a __str__
method must return string value, using __str__
little.. weird. no need override __init__
method, didn't new in it.
you may want name base class other math
don't mask module (and not need import in __str__
method). best practice use camelcase names classes; math
better choice.
for kind of operation, i'd use function instead:
import math def pythagoras(x, y) return math.sqrt(x**2 + y**2)
at best, you'd make pythagoras
method on math class:
import math class math(): def __init__(self, x, y): self.x, self.y = x, y def pythagoras(self): return math.sqrt(self.x ** 2 + self.y ** 2)
Comments
Post a Comment