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