Skip to content

hidden attribute #47

@adewes

Description

@adewes

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

Solutions

Modify the names

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions