Py2K wishes

Paul Prescod paul at prescod.net
Wed Dec 29 08:33:53 EST 1999


Gordon McMillan wrote:
> 
> If you're catching specific methods, why not subclass? 

You cannot subclass objects that are passed to you. Perhaps that is a
way of describing what I want: to be able to subclass arbitrary objects.
I'm trying to generalize the *mechanism* used to do subclassing in
Python (fallback) so that it is available when you don't want the
syntax/semantics of "subclassing".

> OTOH, 
> if your code looks like:
> 
> class Proxy:
>   ...
>   def __getattr__(self, nm):
>     attr = getattr(self.__obj, nm)
>     if type(attr) == type(''):
>        return Unicode(attr)
>     return attr
> 
> then I fail to see how a __fallback__ thingie is going to make 
> your life any easier.

No, my code looks like:

class Proxy:
  ...
  def foo( self, nm ):
     return Unicode2ASCII( self.__obj.foo() )
  def bar( self, nm ):
     return Unicode2ASCII( self.__obj.bar() )
  def __getattr__(self, nm):
     attr = getattr(self.__obj, nm)
     if type(attr) == type(''):
        return Unicode(attr)
     return attr

I want it to look like:

class Proxy:
  ...
  def __init__( self, obj ):
     self.__fallback__=obj
     self.__obj=obj
  def foo( self, nm ):
     return Unicode2ASCII( self.__obj.foo() )
  def bar( self, nm ):
     return Unicode2ASCII( self.__obj.bar() )

 Paul Prescod




More information about the Python-list mailing list