Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __getattribute__ hook and len() problem

Reply
Thread Tools

__getattribute__ hook and len() problem

 
 
ernest
Guest
Posts: n/a
 
      07-16-2010
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      07-16-2010
On Thu, Jul 15, 2010 at 5:42 PM, ernest <(E-Mail Removed)> 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/dat...-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/dat...special-lookup
).***
""" (emphasis mine)

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
ernest
Guest
Posts: n/a
 
      07-16-2010
Thanks Chris & Christian.
Mistery solved

Ernest
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__getattribute__ and methods proxying Giampaolo RodolÓ Python 0 06-12-2010 07:01 PM
Class attributes, instances and metaclass __getattribute__ Pedro Werneck Python 4 08-08-2006 04:35 PM
__getattribute__ and __slots__ pascal.parent@free.fr Python 1 04-17-2006 08:18 AM
newstyle classes and __getattribute__ Stefan Sonnenberg-Carstens Python 5 10-28-2005 10:01 PM
__getattribute__ and __getattr__ Gigi Python 0 05-29-2005 10:43 PM



Advertisments