-2
class learn:

    def __init__(self,radius=1):
        self.radius = radius
     
    def reset_area(self,new_radius):
        self.radius= new_radius
        self.area = new_radius*new_radius*3.14
        self.circum = 2*3.14*radius
        return area + circum  

ERROR - "circum" is not defined. - "area" is not defined.

Nisha
  • 1

2 Answers2

0

When you set a variable with self.variable = 'sth', that variable sets as class property and is accessible in entire class

But when you use variable = 'sth', it's not class property anymore and is accessible in just that function

If you don't want to have area and circum as class property, don't use self:

...
area = new_radius*new_radius*3.14
circum = 2*3.14*new_radius
return area + circum

But if you want to have them outside this method, yes you should use self

Amin
  • 2,605
  • 2
  • 7
  • 15
0

You have not declared a variable whose name is circum, so yes, in this context, you have to refer to the variable you did create, and whose name here is self.circum.

Your code creates two new attributes in the object instance, which is probably not what you want; so in this case, removing the self. prefix from area and circum both would probably be closer to what you actually want (assuming you don't have other methods which assume that these attributes exist; but then your __init__ method should probably create them with some default value, such as perhaps None).

tripleee
  • 175,061
  • 34
  • 275
  • 318