proxy object
Mike Warren
usenet at mike-warren.com
Thu Nov 14 17:08:12 EST 2002
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Greetings,
I have a simplified example of what I want to do (which isn't
working), which basically boils down to this: I want to return a
``proxy'' object which delays the instantiation of
expensive-to-instantiated objects, but I want the object to -- after
the instantiation -- behave just like it would without the proxy.
This mostly works, but isinstance() fails, kind of: if the ``hack''
code below is enabled, then isinstance works but other things fail.
So:
1. is there some way to override isinstance behaviour? or:
2. is there some way to ``replace'' the proxy instance with the
``other'' instance?
3. what exactly is going on below? Why is there an AttributeError
about ``_something''?
Okay, here's the example (I'm obviously not doing the
delayed-instantiation business). Any hints?
,----
| class Proxy:
| def __init__(self,obj,hack=0):
| self._obj = obj
| if hack:
| self.__class__ = self._obj.__class__
|
| def __getattr__(self,name):
| return getattr(self._obj,name)
|
| class Foo:
| def __init__(self):
| self._something = "something"
|
| def something(self):
| return self._something
|
|
| a = Foo()
| b = Proxy(a)
| c = Proxy(a,hack=1)
|
| print a,b,c
|
| print "b is Foo?", isinstance(b,Foo)
| print "c is Foo?", isinstance(c,Foo)
|
| print b.something()
| print c.something()
`----
- --
mike [at] mike [dash] warren.com
<URL:http://www.mike-warren.com>
GPG: 0x579911BD :: 87F2 4D98 BDB0 0E90 EE2A 0CF9 1087 0884 5799 11BD
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 and Gnu Privacy Guard <http://www.gnupg.org/>
iEYEARECAAYFAj3UHqgACgkQEIcIhFeZEb3thQCggqbj3yNK57tb5JGtnci3So0V
PpwAnjnzJUaIc4+JWudSdGFENNzZ80H3
=Covo
-----END PGP SIGNATURE-----
More information about the Python-list
mailing list