CARVIEW |
Select Language
HTTP/2 200
date: Sun, 20 Jul 2025 21:46:37 GMT
server: Apache/2.4.41 (Ubuntu)
vary: Cookie,User-Agent,Accept-Encoding
set-cookie: MOIN_SESSION_443_ROOT_moin=8f5c9a567a07d946db0f989de6dfc6daa0951f2a; Expires=Sun, 20-Jul-2025 22:46:00 GMT; Max-Age=3600; Secure; Path=/
content-encoding: gzip
content-type: text/html; charset=utf-8
x-clacks-overhead: GNU Terry Pratchett
strict-transport-security: max-age=315360000; includeSubDomains; preload
ProxyProgramming - Python Wiki
Proxy
See also https://www.python.org/workshops/1997-10/proceedings/savikko.html
special method names
pro
- ??
cons
- ??
1 # Code is Public Domain.
2 class Proxy(object):
3 def __init__(self, subject):
4 self._subject = subject
5
6 def __getattr__(self, attrName):
7 return getattr(self._subject, attrName)
8
9 def __setattr__(self, attrName, value):
10 object.__setattr__(self, attrName, value)
11
12 def __delattr__(self, attrName):
13 delattr(self._subject, attrName)
14
15 def __call__(self,*args,**keys):
16 object.__getattribute__(self,'_subject')(*args,**keys)
17
18 def __getitem__(self,key):
19 return object.__getattribute__(self,'_subject')[key]
20
21 ## should implement other special method names here
22 ## see https://www.python.org/doc/2.3.2/ref/specialnames.html
23
24 class Test:
25 def __init__(self):
26 self.toto = "toto"
27
28 def foo(self):
29 print "foo"
30
31 def __call__(self,a,b,more=False):
32 print self.__class__.__name__,id(self)
33 print a,b,more
34
35 def __getitem__(self,key):
36 return 'Test%s' % key
37
38 t = Test()
39 p = Proxy(t)
40
41 print p.toto
42 p.foo()
43 p('1',2,more=True)
44 print p['Blabla']
ProxyProgramming (last edited 2009-10-22 20:12:14 by s235-138)
Unable to edit the page? See the FrontPage for instructions.