__getattribute__ hook and len() problem

Discussion in 'Python' started by ernest, Jul 16, 2010.

  1. ernest

    ernest Guest

    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
     
    ernest, Jul 16, 2010
    #1
    1. Advertising

  2. ernest

    Chris Rebert Guest

    On Thu, Jul 15, 2010 at 5:42 PM, ernest <> 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
     
    Chris Rebert, Jul 16, 2010
    #2
    1. Advertising

  3. ernest

    ernest Guest

    Thanks Chris & Christian.
    Mistery solved :)

    Ernest
     
    ernest, Jul 16, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gigi
    Replies:
    0
    Views:
    479
  2. Stefan Sonnenberg-Carstens

    newstyle classes and __getattribute__

    Stefan Sonnenberg-Carstens, Oct 28, 2005, in forum: Python
    Replies:
    5
    Views:
    319
    Stefan Sonnenberg-Carstens
    Oct 28, 2005
  3. Replies:
    1
    Views:
    347
    Peter Otten
    Apr 17, 2006
  4. Tor Erik Soenvisen
    Replies:
    14
    Views:
    593
    Tim Roberts
    Nov 23, 2006
  5. maestro
    Replies:
    1
    Views:
    324
    Chris
    Aug 11, 2008
Loading...

Share This Page