decoupling a certain method

castironpi at gmail.com castironpi at gmail.com
Fri Mar 21 03:32:40 EDT 2008


class Proxy:
   def __init__( self, proxer ):
      object.__setattr__( self, '_proxer', proxer )

ComplexObject= object

viewA= type( 'ComplexView', ( Proxy, ComplexObject ), { '__eq__':
lambda self, other: abs( self._proxer- other )<= 1 } )

j= range( 10 )

g= 5
for a in j:
   p= viewA( a )
   print( p.__class__.__name__, p._proxer, p== g )


Subclassing provides an alternative to delegation and proxying.  I got
this far, but I'm lost: How do I allow g= proxy( 5 ) in line 11?  Do I
need to?

If I do it, I get:

TypeError line 7: unsupported operand type(s) for -: 'int' and
'ComplexView'



More information about the Python-list mailing list