When i was trying to use example from textbook and trying to rearrange some lines of code for executing, I found the result was not what I expected.
There is a ''class'' which looks like below.
class Circle:
"""This class creates circle"""
pi = 3.14159
all_circles = []
def __init__(self, radius):
self.radius = radius
self.__class__.all_circles.append(self)
def area(self):
return self.__class__.pi * (self.radius ** 2)
@staticmethod
def total_area():
total =0
for circle in Circle.all_circles:
total += circle.area()
return total
@classmethod
def total_area2(cls):
total = 0
for circle in cls.all_circles:
total += circle.area()
return total
When I execute it.....
c1 = Circle(10)
c2 = Circle(15)
print(c1.__class__.total_area()) # get 1021.01675
print(c2.__class__.total_area()) # get 1021.01675
In my opinion, Both c1 and c2 should be independent objects.
How do the codes cause 1021.01675 by c1 and 1021.01675 by c2
but.....
c1 = Circle(10)
print(c1.__class__.total_area()) # get 314.159
c2 = Circle(15)
print(c2.__class__.total_area()) # get 1021.01675
Do I ingnore, forget or misunderstand some concepts?
My position on this problem is that the execution should be looked like....
c1 = Circle(10)
c2 = Circle(15)
print(c1.__class__.total_area()) # get 314.159
print(c2.__class__.total_area()) # get 706.85775
and.....
c1 = Circle(10)
print(c1.__class__.total_area()) # get 314.159
c2 = Circle(15)
print(c2.__class__.total_area()) # get 706.85775