__getattribute__ hook and len() problem

Chris Rebert clp2 at rebertia.com
Thu Jul 15 20:51:56 EDT 2010


On Thu, Jul 15, 2010 at 5:42 PM, ernest <nfdisco at 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



More information about the Python-list mailing list