adewes / blitzdb

Blitz is a document-oriented database for Python that is backend-agnostic. It comes with a flat-file database for JSON documents and provides MongoDB-like querying capabilities.
http://blitzdb.readthedocs.org
MIT License
330 stars 37 forks source link

hidden attribute #47

Open adewes opened 9 years ago

adewes commented 9 years ago

An attribute and a method of a class share the same name. Give unique names to the two objects or else Python will raise a TypeError: object is not callable error at runtime. Python raises the error because the name is associated to the attribute, so when you insert parentheses after the name, Python actually tries to call the attribute (although you were trying to call the function).

Affected files

Per Python style conventions, the author modified his Rectangle class by deleting the area method. Whenever a module needs to access the area attribute of a Rectangle instance, the module can just access the area attribute directly. This also suppresses the Attribute hides this method error.

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.area = width * height
    # deleted area method from here

r = Rectangle(3, 4)
print r.area  # access the attribute directly now

References