2001 Enchancement Wishlist

Andrew Dalke dalke at acm.org
Sat Jan 6 12:32:19 EST 2001


Alex Martelli:
>Recognizing (e.g.) a "def __setattr__" at module-level as providing
>a similar function to a similarly-named method in a class instance
>would (in my opinion) not 'complicate' the Python language: it would
>just extend an already-existing concept (which currently only applies
>to class-instances, and modules coded in C) to some more objects
>(to wit, modules coded in Python).  It would just add a modest amount
>of functionality at an even more modest conceptual cost.

I wrote a system once which proxies local functions to a remote
machine.  It only used strings, floats and ints so pass by copy
worked and I used pickle.

The idea was that I wanted to do something like

import remote
remote.proxy("math", remote.RshConnection("other.machine"))
import math
math.cos(0.0)

I wanted module level __getattr__ and __setattr__ to do this
but found I could implement remote.proxy as

class Proxy:
  def __init__(self, (to_remote, from_remote)):
    self.to_remote = to_remote
    self.from_remove = from_remote
    # ask the remote server for a list of all constants
    #   and their values.
    # ask the remote server for a list of functions
    ...
  def __getattr__(self, key):
    # if it is a constant, return the value
    # if it is a function, return a __call__ able proxy wrapper
    # else raise an AttributeError

def proxy(module_name, connection):
  import sys
  proxy = Proxy(...)
  sys.modules[module_name] = proxy

In other words, what you import doesn't need to be a module.
It can be a class instance instead - or any other data type.
This makes it possible to emulate the module __getattr__
behaviour without changing the definition of a module.

I suppose a modified import could also be written to wrap
every module inside of a proxy instance that forwards calls
to the underlying module, and forwards __getattr__/__setattr__
calls to the appropriate function.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list