T
TP
Hi everybody,
I try to make a "link" (or shortcut, if you want) so that len(b_instance)
computes in fact len(a) (see the code below). I could write a method
__len__ to b, but I wonder if it is possible to make it with a
setattr/getattr trick, as I do below.
Thanks in advance,
Julien
######################
class b( object ):
def __init__( self ):
super( b, self ).__init__()
a = [5,4,7]
b_instance = b()
setattr( b_instance, "__len__", getattr( a, "__len__") )
print len( a )
print id( a.__len__ )
print hasattr( b_instance, "__len__" )
print id( b.__len__ )
print len( b_instance )
########################
I obtain:
$ p test_setattr.py
3
3083822540
True
Traceback (most recent call last):
File "test_setattr.py", line 14, in <module>
print id( b.__len__ )
AttributeError: type object 'b' has no attribute '__len__'
Command failure: error 1 !
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
I try to make a "link" (or shortcut, if you want) so that len(b_instance)
computes in fact len(a) (see the code below). I could write a method
__len__ to b, but I wonder if it is possible to make it with a
setattr/getattr trick, as I do below.
Thanks in advance,
Julien
######################
class b( object ):
def __init__( self ):
super( b, self ).__init__()
a = [5,4,7]
b_instance = b()
setattr( b_instance, "__len__", getattr( a, "__len__") )
print len( a )
print id( a.__len__ )
print hasattr( b_instance, "__len__" )
print id( b.__len__ )
print len( b_instance )
########################
I obtain:
$ p test_setattr.py
3
3083822540
True
Traceback (most recent call last):
File "test_setattr.py", line 14, in <module>
print id( b.__len__ )
AttributeError: type object 'b' has no attribute '__len__'
Command failure: error 1 !
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)