Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web.
|
2006-01-11 Recording attribute access and method callsHere's a little bit of magic, demonstrated in the Python interpreter:
>>> from record import Record
>>> r = Record()
>>> r2 = r.a.b('abc').c
>>> print r2
a.b('abc').c
>>> print r.d.e(r2, t=20).f()
d.e(Record().a.b('abc').c, t = 20).f()
Rather than making function calls or giving you real attributes, the
A version of this cropped up in PingingWeblogsInPython, where it's the underlying mechanism the xmlrpclib module uses to let people invoke XML/RPC procedures as if they were writing plain Python calls. Python allows objects to customize access to themselves (covered here), so the implementation is pretty simple:
This creates a new object for each step of the path, but because
Because Python doesn't distinguish between 'call attribute' and
'access attribute', all of the attributes you get back are potentially
callable (eg, Sidebar: the actual codeHere's a simple implementation of the
(As usual, some names have been shortened to make the code not take up too much space.) In a real version,
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |