[Python-Dev] Data descriptor doc/implementation inconsistency (original) (raw)

Amaury Forgeot d'Arc amauryfa at gmail.com
Mon Jan 11 01:51:09 CET 2010


Hi,

2010/1/11 Benjamin Peterson <benjamin at python.org>:

Consider this program:

class Descr(object):  def init(self, name):  self.name = name  def set(self, instance, what):  instance.dict[self.name] = what class X(object):  attr = Descr("attr") x = X() print(x.attr) x.attr = 42 print(x.attr) It gives in output: <_main_.Descr object at 0x7fe1c9b28150> 42 The documentation [1] says that Descr is a data descriptor because it defines the set method. It also states that data descriptors always override the value in the instance dictionary. So, the second line should also be the descriptor object according to the documentation. My question is: Is this a doc bug or a implementation bug? If the former, it will be the description of a data descriptor much less consistent, since it will require that a get method be present, too. If the latter, the fix may break some programs relying on the ability to "cache" a value in the instance dictionary. [1] http://docs.python.org/reference/datamodel#invoking-descriptors

Quoting the documentation: """Normally, data descriptors define both get() and set(), while non-data descriptors have just the get() method. """ Your example is neither a data descriptor nor a non-data descriptor...

The thing that worries me a bit is the "x.attr" returning the Descr object. Descriptors should remain at the class level, and instance should only see values. I'd prefer an AttributeError in this case.

-- Amaury Forgeot d'Arc



More information about the Python-Dev mailing list