(Python) How to access/use variable from different method in class? -
here's code
class circle(object): def __init__(self, radius = 1): self_radius = radius def __str__(self): return "circle radius {}".format(self_radius)
i took teacher's slide, took integer (radius) when called (a = circle(25) -for example) return --circle radius 25-- when print it
the problem when that, instead getting it, got error saying self_radius not defined (in str method), question how use variable in different method it's origin?
thank you
qualify instance attributes self.
, not self_
:
class circle(object): def __init__(self, radius = 1): self.radius = radius def __str__(self): return "circle radius {}".format(self.radius)
if name variable self_radius
, become local variable; not accessible method.
Comments
Post a Comment