From: ernest on
Hi!

I have this class that overrides the __getattribute__ method,
so that it returns the attributes of str(self) instead of the
attributes of self.

class Part(object):
def __init__(self):
self.content = []
def __str__(self):
return str.join('\n', self.content)
def __getattribute__(self, name):
if name in ['content', 'write', '__str__']:
return object.__getattribute__(self, name)
else:
return str(self).__getattribute__(name)
def write(self, data):
self.content.append(data)

Then I do:

In [50]: p = Part()

In [51]: p.write('foo')

In [52]: p.upper()
Out[56]: 'FOO'

This is okay, works as expected.

However, len(p) fails:

TypeError: object of type 'Part' has no len()

And yet, p.__len__() returns 3. I though len(object) simply
called object.__len__.

Can somebody shed some light on this??

Many thanks in advance.

Ernest
From: Chris Rebert on
On Thu, Jul 15, 2010 at 5:42 PM, ernest <nfdisco(a)gmail.com> wrote:
> Hi!
>
> I have this class that overrides the __getattribute__ method,
> so that it returns the attributes of str(self) instead of the
> attributes of self.
>
> class Part(object):
>    def __init__(self):
>        self.content = []
>    def __str__(self):
>        return str.join('\n', self.content)
>    def __getattribute__(self, name):
>        if name in ['content', 'write', '__str__']:
>            return object.__getattribute__(self, name)
>        else:
>            return str(self).__getattribute__(name)
>    def write(self, data):
>        self.content.append(data)
>
> Then I do:
>
> In [50]: p = Part()
>
> In [51]: p.write('foo')
>
<snip>
> However, len(p) fails:
>
> TypeError: object of type 'Part' has no len()
>
> And yet, p.__len__() returns 3. I though len(object) simply
> called object.__len__.
>
> Can somebody shed some light on this??

Quoth http://docs.python.org/reference/datamodel.html#more-attribute-access-for-new-style-classes
:
"""
3.4.2.1. More attribute access for new-style classes

object.__getattribute__(self, name)
<snip>
***Note: This method may still be bypassed when looking up special
methods as the result of implicit invocation via language syntax or
built-in functions. See Special method lookup for new-style classes
(http://docs.python.org/reference/datamodel.html#new-style-special-lookup
).***
""" (emphasis mine)

Cheers,
Chris
--
http://blog.rebertia.com
From: Christian Heimes on
> And yet, p.__len__() returns 3. I though len(object) simply
> called object.__len__.

Not exactly, all __magic__ methods of new style classes are called like
getattr(type(obj), "__len__")(obj). As a result magic methods are never
looked up on the object, including hooks like __getattr_() and
__getattribute__().

Christian

From: ernest on
Thanks Chris & Christian.
Mistery solved :)

Ernest
 | 
Pages: 1
Prev: Fairy scenes
Next: Toronto PyCamp 2010