Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a property object?

Tags:

I am not new to python, but I have a pretty basic question here.

I was playing around with python and found that there is the type property

>>> property <type 'property'> 

But I have only heard of properties in the function context.

>>> a = property() <property object at 0x0246C090> 

But what about property objects? What are they use? Property methods are not very intuitive or suggestive

>>> dir(a) ['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter'] 

Thank you for the attention!

like image 880
rafaelc Avatar asked May 04 '15 18:05

rafaelc


People also ask

What is a property object Python?

A property is an attribute object containing a getter and a setter method.

What is property name in object?

identifier is the name of the property to access and expression should evaluate to an object. After the destructuring, the variable identifier contains the property value. const { name } = hero is an object destructuring. The destructuring defines a variable name with the value of property name .

What is an object property in ontology?

In an ontology, the class hierarchy framework can be extended with more open-ended relationships, called object and data properties. Object properties connect two individuals (a subject and object) with a predicate, while with data properties the predicate connects a single subject with some form of attribute data.

What is property of object in JavaScript?

Properties are the values associated with a JavaScript object. A JavaScript object is a collection of unordered properties. Properties can usually be changed, added, and deleted, but some are read only.


1 Answers

The property object is what you are actually thinking of as a property. Consider this example:

class Foo(object):     def __init__(self):         self._bar = 0      @property     def bar(self):         return self._bar + 5 

Foo.bar is a property object which has a __get__ method. When you write something like

x = Foo() print(x.bar) 

the lookup for x.bar finds that type(x).bar has a __get__ method, and so the attribute lookup becomes equivalent to

type(x).bar.__get__(x, type(x)) 

which produces the value x._bar + 5.

The use of property as a decorator somewhat obscures the fact that bar is a property object. An equivalent defintion is

class Foo(object):      def __init__(self):          self._bar = 0       bar = property(lambda self: self._bar + 5) 

which shows more explicitly that you are creating a property object with the given lambda expression as the getter for that property, and binding the object to the class attribute bar.

The property class (along with instance methods, class methods, and static methods) is a specific application of Python's general descriptor protocol, which defines the behavior of class attributes with __get__, __set__, and/or __del__ methods.

like image 56
chepner Avatar answered Nov 26 '22 10:11

chepner