This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Polymorphic getters / setters
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, amaury.forgeotdarc, trialcode
Priority: low Keywords:

Created on 2005-12-23 13:08 by trialcode, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg61215 - (view) Author: Adde (trialcode) Date: 2005-12-23 13:08
If you add a property to a class with a getter and/or
setter and override the getter and/or setter in a
subclass the baseclass implementation of the methods is
still called when the property is accessed on objects
of the subclass (see below for example).
This feels like a pretty arbitrary limitation that
prevents overriding the behaviour of properties like
you would with a normal method. I'm sure there's a way
to make the property search the inheritance-hierarchy
for the provided method signature when called.

class base(object):
def get_foo(self):
print "Base get"
def set_foo(self, value):
print "Base set"
foo = property(get_foo, set_foo)

class sub(base):
def get_foo(self):
print "Sub get"
def set_foo(self, value):
print "Sub set"

s = sub()
s.foo = s.foo

-- Prints:

Base get
Base set
msg83887 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-03-20 23:30
Confirmed on trunk and py3k, not sure it's a bug.
msg89939 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-06-30 16:34
This is normal behavior: the property is created with the functions
created just above, even before they belong to any class.

To make the property search the class hierarchy, you could write: 
  foo = property(lambda x: x.get_foo(), lambda x, v: x.set_foo(v))

or make it a function:
  def polymorphic_property(getter, setter):
      return property(lambda x  : getattr(x, getter)(),
                      lambda x,v: getattr(x, setter)(v))
  [...]
  foo = polymorphic_property('get_foo', 'set_foo')
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42720
2009-06-30 16:34:14amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg89939

resolution: works for me
2009-03-20 23:30:50ajaksu2setpriority: normal -> low
versions: + Python 3.1, Python 2.7
nosy: + ajaksu2

messages: + msg83887
2005-12-23 13:08:36trialcodecreate