__getattribute__ hook and len() problem

E

ernest

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
 
C

Chris Rebert

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')
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top