0

From Python in a Nutshell

A property is an instance attribute with special functionality. ...

Here’s one way to define a read- only property:

class Rectangle(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def get_area(self):
        return self.width * self.height
    area = property(get_area, doc='area of the rectangle')

Each instance r of class Rectangle has a synthetic read-only attribute r.area , computed on the fly in method r.get_area() by multiplying the sides.

Is a property a class attribute or an instance attribute?

  1. Does the above quote imply that a property is an instance attribute?

  2. A property is always defined inside the definition of a class, so is a property a class attribute.

  3. Does Rectangle.__dict__ store the class attributes and an Rectangle instance's __dict__ store the instance attributes? If yes, then does the following show that the property is a class attribute instead of an instance attribute:

>>> Rectangle.__dict__    
mappingproxy({..., 'area': <property object at 0x7f34f7ee2818>})
>>> r=Rectangle(2,3)
>>> r.__dict__    {'width': 2, 'height': 3} 
4
  • 2
    The property object itself is an attribute of the class, that's how it works (see docs.python.org/3/howto/descriptor.html). I guess that's why it's referred to as synthetic. Please ask one thing at a time and do research (see e.g. stackoverflow.com/q/20201029/3001761 for the second question I've removed). Commented Jun 27, 2017 at 21:40
  • Thanks. (1) Does "synthetic" mean class attribute? (2) Does r.area imply area is an instance attribute of instance r? Commented Jun 27, 2017 at 21:45
  • 1
    I not sure I understand either of those questions. 1. Synthetic means artificial. 2. You're accessing it on the instance, sure, but you can always do that with class attributes. Commented Jun 27, 2017 at 21:50
  • "synthetic" just seems like a word choice by whoever wrote those docs to indicate "not really". r.area, if it returns something, implies that area is an attribute of the instance, or the class, or any of the classes in the MRO Commented Jun 27, 2017 at 21:52

1 Answer 1

2

The property object itself is a class attribute, as its position inside the class body implies; you can still access class attributes on instances, though, just not vice versa.

What makes the property different is the descriptor protocol; in the case of accessing r.area, the following (roughly) happens:

  • Python looks for the attribute on the instance, and doesn't find it; then
  • Python looks for the attribute on the instance's type, and does find it; then
  • Python finds that the resulting value implements __get__, so;
  • Python invokes that method with the instance and the class.

What actually gets called is therefore:

Rectangle.area.__get__(r, Rectangle)

This is how the property descriptor accesses the instance's state while actually being an attribute on the class; the instance is passed into it. You can tell it's a class attribute because it's accessible on the class, without creating any instances:

>>> Rectangle.area
<property object at 0x...>
Sign up to request clarification or add additional context in comments.

4 Comments

See my edit. I just found the book said "A property is an instance attribute with special functionality." Also do __dict__ of the class and of an instance store class attributes and instance attributes respectively?
@Tim the property object is a class attribute, as I state in my answer, as its definition tells you and, yes, as which __dict__ it's in demonstrates.
Thanks. Is a property a class attribute only used for simulating an instance attribute? Is it used for any other purpose?
@Tim like what? I mean, I can't speak to what everyone everywhere is doing with it, but that's what it's primarily for. Please read the documentation, which suggests no alternative uses. Frankly it's not clear to me what the scope of what you're expecting is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.