property confusion

K

K Richard Pixley

Could someone please explain to me why the two values at the bottom of
this example are different?

Python-3.3 if it makes any difference.

Is this a difference in evaluation between a class attribute and an
instance attribute?

--rich

class C:
def __init__(self):
self._x = None

def getx(self):
print('getx')
return self._x
def setx(self, value):
print('setx')
self._x = value
def delx(self):
print('delx')
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")

class D:
def getx(self):
print('getx')
return self._x
def setx(self, value):
print('setx')
self._x = value
def delx(self):
print('delx')
del self._x

def __init__(self):
self._x = None
self.x = property(self.getx, self.setx, self.delx, "I'm the 'x'
property.")

type(C().x)
type(D().x)
 
R

Rotwang

Could someone please explain to me why the two values at the bottom of
this example are different?

Python-3.3 if it makes any difference.

Is this a difference in evaluation between a class attribute and an
instance attribute?

Yes, see below.

--rich

class C:
def __init__(self):
self._x = None

def getx(self):
print('getx')
return self._x
def setx(self, value):
print('setx')
self._x = value
def delx(self):
print('delx')
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")

class D:
def getx(self):
print('getx')
return self._x
def setx(self, value):
print('setx')
self._x = value
def delx(self):
print('delx')
del self._x

def __init__(self):
self._x = None
self.x = property(self.getx, self.setx, self.delx, "I'm the 'x'
property.")

type(C().x)
type(D().x)

Properties are implemented as descriptors, i.e. objects that have
__get__, __set__ and/or __delete__ methods:

http://docs.python.org/3/reference/datamodel.html#invoking-descriptors

If a is an instance and type(a) has an attribute named 'x' which is a
descriptor then a.x is transformed into

type(a).__dict__['x'].__get__(a, type(a));

in the case of your code, C.__dict__['x'] is the property you defined,
and its __get__ method is just getx. But because of the way attribute
lookup works for descriptors, properties don't work as attributes of
instances as is the case with D(); D().x just gives you
D().__dict__['x'] which is the property itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top