Submitted by Justin (not verified) on May 18, 2007 - 14:29.
OnDemandPhoto can be written differently by overriding __getattr__
I did this recently to cache an RPC call that returned many different fields.
it basically did something like
def _get_remote_info(self):
ifself._remote_info:
returnself._remote_info
self._remote_info = do_rpc_call_stuff(self.id)
returnself._remote_info
remote_info = property(_get_remote_info)
def__getattr__(self, name):
if name in('foo', 'bar','baz','lots','of','fields'):
returnself.remote_info[name]
...
#alternatively: if name in self.remote_info:
# return self.remote_info[name]
This way, if the remote method was changed to return more values, I only had to change one line, or zero if I used the alternate method. The only downside I see to this is that the autogenerated docs for the class won't include any mention of these fields.
Reply
Contact
Email
colin@colinm.org
Lab
Wean Hall 1302
(412) 268-2601
Office
Wean Hall 1307
(412) 268-4751
Snail Mail
Colin McMillen
Carnegie Mellon Univ.
Computer Science Dept.
5000 Forbes Avenue
Pittsburgh, PA 15213
U.S.A.
OnDemandPhoto can be written
OnDemandPhoto can be written differently by overriding __getattr__
I did this recently to cache an RPC call that returned many different fields.
it basically did something like
This way, if the remote method was changed to return more values, I only had to change one line, or zero if I used the alternate method. The only downside I see to this is that the autogenerated docs for the class won't include any mention of these fields.