[issue21814] object.__setattr__ or super(...).__setattr__?

2014-06-20 Thread Vincent Besanceney

New submission from Vincent Besanceney:

In: 
https://docs.python.org/2.7/reference/datamodel.html#customizing-attribute-access

Regarding the description of __setattr__ method: "For new-style classes, rather 
than accessing the instance dictionary, it should call the base class method 
with the same name, for example, object.__setattr__(self, name, value)."

Wouldn't it be more consistent for new-style classes, instead of calling 
"object.__setattr__(self, name, value)", to call "super(, 
self).__setattr__(name, value)"?

--
assignee: docs@python
components: Documentation
messages: 221082
nosy: docs@python, vincentbesanceney
priority: normal
severity: normal
status: open
title: object.__setattr__ or super(...).__setattr__?
type: enhancement
versions: Python 2.7

___
Python tracker 
<http://bugs.python.org/issue21814>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21814] object.__setattr__ or super(...).__setattr__?

2014-06-21 Thread Vincent Besanceney

Vincent Besanceney added the comment:

If I understand it right, in a simple case like this:

  class Foo(object):

def __setattr__(self, name, value):
  # some logic, then...
  super(Foo, self).__setattr__(name, value)

calling super is equivalent to calling object.__setattr__, but doesn't have any 
added-value since there's no cooperative multiple inheritance involved, in that 
case we are better off calling the parent class method directly.

If Foo evolves to have multiple ancestors, object has a __setattr__ method and 
that object is always the last class in the MRO chain, so any sequence of calls 
to super(..).__setattr__ is guaranteed to end with a call to object.__setattr__ 
method (assuming ancestors __setattr__ method, if any, calls super as well), 
and that may be what we want, i.e. to also benefit from an ancestor __setattr__ 
method.

Hmm, this last point may be too specific if not out of scope in regards to the 
documentation. Falling back to the original need, from the documentation "If 
__setattr__() wants to assign to an instance attribute...", so as you said, 
"calling object.__setattr__ gives a known, guaranteed behavior".

--

___
Python tracker 
<http://bugs.python.org/issue21814>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com