<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=windows-1252"
 http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Michael Foord a écrit :
<blockquote cite="mid:4BA576AF.2050208@voidspace.org.uk" type="cite">
  <meta content="text/html; charset=windows-1252"
 http-equiv="Content-Type">
On 20/03/2010 12:00, Pascal Chambon wrote:
  <blockquote cite="mid:4BA4B8DE.8060109@wanadoo.fr" type="cite">
    <meta content="text/html; charset=windows-1252"
 http-equiv="Content-Type">
    <br>
    <br>
But the point which for me is still unclear, is : does the default
implementation of __getattribute__ (the one of "object") call
__getattr__ by himself, or does it rely on its caller for that, by
raising an AttributeError ? For Python2, it's blatantly the latter case
which is favoured, but since it looks like an implementation detail at
the moment, I propose we settle it (and document it) once for all.<br>
  </blockquote>
  <br>
Ah right, my apologies. So it is still documented behaviour -
__getattr__ is obviously called by the Python runtime and not by
__getattribute__. (It isn't just by getattr as the same behaviour is
shown when doing a normal attribute lookup and not via the getattr
function.)<br>
  <br>
</blockquote>
I really don't see the docs you're referring to ; until I tested
myself, I think I had no obvious reasons to guess that __getattribute__
relied on the upper level caller instead of finishing the hard job
himself.<br>
<br>
<br>
Nick Coghlan a écrit :
<blockquote cite="mid:4BA57CC1.1050404@gmail.com" type="cite">
  <pre wrap="">Michael Foord wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Well, the documentation you pointed to specifies that __getattr__ will
be called if __getattribute__ raises an AttributeError, it just doesn't
specify that it is done by object.__getattribute__ (which it isn't).
    </pre>
  </blockquote>
  <pre wrap=""><!---->
And as for why not: because __getattribute__ implementations need to be
able to call object.__getattribute__ without triggering the fallback
behaviour.

Cheers,
Nick.

  </pre>
</blockquote>
I guess there are cases in which it is beneficial indeed.<br>
<br>
<blockquote cite="mid:4BA5A67A.9000703@canterbury.ac.nz" type="cite"> <br>
Michael Foord wrote: <br>
  <br>
  <blockquote type="cite">Well, the documentation you pointed to
specifies that __getattr__ will be called if __getattribute__ raises an
AttributeError, it just doesn't specify that it is done by
object.__getattribute__ (which it isn't).
    <br>
  </blockquote>
  <br>
If __getattribute__ raises an exception, it won't get a chance to <br>
do anything else, so something outside of __getattribute__ must <br>
catch the AttributeError and calling __getattr__. So I think the <br>
docs *are* specifying the behaviour here, if only by implication. <br>
  <br>
</blockquote>
I don't follow you there - in my mind, the default __getattribute__
could simply have wrapped all its operations inside soem kind of
"try..catch AttributeError:" mechanism, and thus been able to fallback
to __getattr__ in any way.<br>
<br>
<br>
If I sum it up properly the semantic is :<br>
-A.obj and getattr(A, "obj") are exactly the same<br>
-They trigger the calling of __getattribute__ on the object (or it's
python core equivalent)<br>
-By default, this __getattribute__ browse the whole object hierarchy
according to well known rules (__dict__, type,  type's ancestors..),
handling descriptor protocols and the like. But it doesn't fallback to
__getattr__ - it raises an AttributeError instead.<br>
-getattr() falls back to __getattr__ if __getattribute__ fails<br>
-customized __getattribute__ methods have the choice between calling
__getattr__ by themselves, or delegating it to getattr() by raising an
exception.<br>
<br>
Wouldn't it be worth completing the doc with these point ? They really
didn't seem obvious to me basically (even though, after analysis, some
behaviours make more sense than others).<br>
I might submit a patch.<br>
<br>
regards, <br>
Pascal<br>
<br>
<br>
<blockquote cite="mid:4BA576AF.2050208@voidspace.org.uk" type="cite">
  <pre class="moz-signature" cols="72">
  </pre>
</blockquote>
<br>
</body>
</html>