FAQ: Learn Python: Classes - Self

class Circle:
  pass
hi = Circle()
hi.hi = 'hi'
print(hi.hi)
print(Circle().hi)

It shows like this
hi
Traceback (most recent call last):
File “script.py”, line 7, in
print(Circle().hi)
AttributeError: ‘Circle’ object has no attribute ‘hi’

what’s the reason for this error

print(Circle().hi)

Did you try,

print (Circle.hi)

?


yes , i have .
thanks for your responce.
print(Circle.hi) shows the same result .

Did you try what was earlier suggested?

When their is no initializer in the class, any attributes we arbitrarily apply to an instance will also belong to the Class.

class Circle:
    pass

a = Circle()
b = Circle()

a.area = 10

print (a.area)         # 10
print (b.area)         # 10
print (Circle.area)    # 10

So you see we have not assigned a unique value to the area attribute, but a universal one. That is why it is so important to give the class an __init__() method so all instances are unique from each other, and the class.

1 Like


yes, i have tried it here is that
and when i introduce class variable “”" hi = “anything” “”"
It prints the the class variable without error but only for the last print statement . The first statement was the same as in the picture. for both(Class( ).hi and Class.hi )

What happens when you use my example from above?


I have understood what you have explained above.
area attribute has a value of 16 when we even use with other object . Am I correct .
See it even shows error with b.area statement.
whatever comes goes error remains the same.

Okay, we walk it back. When we gave a and area attribute, it did not actually become a class variable. My bad. It’s what happens when we toss something together and expect it to work without testing it. Big no-no.

Let’s work this around…

>>> Circle.area = 100
>>> a.area
10
>>> b.area
100
>>> 

Also, any new instance will have the same area.

>>> c = Circle()
>>> c.area
100
>>> 

Now we see the real effect of defining a class attribute as opposed to an instance attribute. But it would tedious if we had to do this all the time with every new instance. That is why we initialize.

>>> from math import pi
>>> class Circle:
    PI = float(f"{pi:.5f}")
    def __init__(self, radius):
        self.r = radius
        self.area = self.calc_area()
        self.circumference = self.calc_circumference()
    def calc_circumference(self):
        return self.r * 2 * self.PI
    def calc_area(self):
        return self.r ** 2 * self.PI

>>> a = Circle(10)
>>> b = Circle(20)
>>> c = Circle(30)
>>> a.area
314.159
>>> b.area
1256.636
>>> c.area
2827.431
>>> a.circumference
62.8318
>>> b.circumference
125.6636
>>> c.circumference
188.4954
>>> 

Now we can see how the class itself can have transient instances…

>>> Circle(5).area
78.53975
>>> Circle(5).circumference
31.4159
>>> 
1 Like


According to understanding - when we use class.attr(circle.area) which is not a variable (area) but after introducing that ;a variable is created and it can be used for all objects.
Okay after that when we use a particular object by using variable it can be edited or changed .
Here the question arises what’s the diff b/t class.attr and class( ).attr because when I use

circle.area = 100
print(circle.area)
#prints 100

but

circle( ).area = 100
print(circle( ).area)
#prints the attr error

Am I correct for the above words which i understand?

Only if it is defined as a class variable, else none of the instances will have that attribute.

1 Like

The first assigns the attribute, the second is an attribute of the instance of the class. Note above that we supplied an argument to the transient instance.

1 Like

Thank you so much for your explanation and spending your time with us. I have saw your explanation from the first lesson of python. I have understood very well on sharing my queries with you . once again thank you for showering your knowledge.

1 Like