Skip to content

Instantly share code, notes, and snippets.

@naufraghi
Forked from anonymous/property names
Last active December 20, 2015 04:19
Show Gist options
  • Save naufraghi/6070273 to your computer and use it in GitHub Desktop.
Save naufraghi/6070273 to your computer and use it in GitHub Desktop.
# This is the "normal" way
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
return self._x
@x.setter # will copy C.x and add the setter
def x(self, value):
self._x = value
@x.deleter # will copy the new C.x and add the deleter
def x(self):
del self._x
# This is an history preserving way
class C(object):
def __init__(self):
self._x = None
@property
def get_x(self):
return self._x
@get_x.setter # will copy C.get_x and add the setter
def set_x(self, value):
self._x = value
@set_x.deleter # will copy C.set_x and add the deleter
def x(self): # The last name is the one we will use
del self._x
@naufraghi
Copy link
Author

So, long story short, what really happens in the code is that every @x.[setter|setter|deleter] makes a copy of the referred property and plugs it in the new name.

https://gist.github.com/naufraghi/6070273

http://svn.python.org/view/python/trunk/Objects/descrobject.c?view=markup#l1211

What I miss is why a copy is done instead of modifying the first defined property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment