FEEDBACK WANTED: Type/class unification

Jiba a11w at SoftHome.net
Mon Aug 13 10:21:57 CEST 2001


Guido van Rossum wrote:
> That never worked, and still won't work.  I don't see that as a loss.

Yes it does not work... I've never tried this, but i have assumed it
work... sorry for that.

> > By the way, it's interesting to notice that, for wrapper, o.method() is
> > not the same think than O.method(o)...
> 
> In what sense?

if O.method(arg) call, say, arg.something, if you pass the wrapper o,
the method call "something" will be wrapped. If you pass the non-wrapped
object oo, it won't be.

For example we can do a ReadOnlyWrapper that's wrap an object and make
it read-only :

class ReadOnly:
  def __init__(self, object):
    self.wrappedobject = object

  def __getattr__(self, name):
    return getattr(self.wrappedobject, name)

  def __setattr__(self, name, value):
    if name == "wrappedobject": self.__dict__["wrappedobject"] = value
    else: pass

This code DOESN'T WORK if wrappedobject has, for example, a setX(newX)
method, because :

>>> object = [what ever you want]
>>> ReadOnlyWrapper = ReadOnly(object)
>>> ReadOnlyWrapper.setX(1)

will modify the object. The only way to fix that may be to call :

>>> ReadOnly.setX(ReadOnlyWrapper, newX)

We may change __getattr__ so it does convert automatically
"ReadOnlyWrapper.setX(1)" into "ReadOnly.setX(ReadOnlyWrapper, newX)".


So it can really be usefull in some very particular cases !

> --Guido van Rossum (home page: http://www.python.org/~guido/)

Jiba





More information about the Python-list mailing list