RBVanDyke (4) [Avatar] Offline
#1
In the Quick Python Book, both print and online versions, section 15.5.1 lists the following code sample which is contained in file circle.py:

"""circle module: contains the Circle class."""
class Circle:
    """Circle class"""
    # Variable containing a list of all circles which have been created.
    all_circles = []
    pi = 3.14159
    def __init__(self, r=1):
        """Create a Circle with the given radius"""
        self.radius = r
        self.__class__.all_circles.append(self)
    def area(self):
        """determine the area of the Circle"""
        return self.__class__.pi * self.radius * self.radius

@staticmethod
def total_area():
    total = 0
    for c in Circle.all_circles:
        total = total + c.area()
    return total

The book then directs the reader to "...interactively type the following":
>>> import circle
>>> c1 = circle.Circle(1)
>>> c2 = circle.Circle(2)
>>> circle.Circle.total_area()

The fourth command, circle.Circle.total_area(), produces the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: type object 'Circle' has no attribute 'total_area'


Any ideas how to fix this?
RBVanDyke (4) [Avatar] Offline
#2
15.5.1 File circle.py doesn't run as listed?
OK, I indented the entire @staticmethod block and finally got this to work. So it appears that the code sample might have an error(?)
naomi.ceder (74) [Avatar] Offline
#3
Sorry I missed this earlier. Yes, you are right, the code example in the book was formatted incorrectly.

Thanks for posting and then solving this!

Naomi Ceder