pythonpropertiesattributes

What's the difference between a Python "property" and "attribute"?


I am generally confused about the difference between a "property" and an "attribute", and I can't find a great resource to concisely detail the differences.


Solution

  • Properties are a special kind of attribute. Basically, when Python encounters the following code:

    spam = SomeObject()
    print(spam.eggs)
    

    it looks up eggs in SomeObject1, and then examines eggs to see if it has a __get__, __set__, or __delete__ method -- if it does, it's a property, and Python will call the __get__ method (since we were doing lookup) and return whatever that method returns. If it is not a property, then eggs is looked up in spam, and whatever is found there will be returned.

    More information about Python's data model and descriptors.


    1 Many thanks to Robert Seimer for the correction on the lookup sequence.