On 7/22/10 3:34 PM, wheres pythonmonks wrote: > Now, I want to make the above codes more "Pythonic" -- is there a way to: > > 1. Get the function's arguments from the perspective of the caller? > > def f(x): > print "caller's view of x = %s" % callersview(x) > > Then, f(1+2+3) would yield: > caller's view of x = 1 + 2 + 3
No. You can use frame hacking to dig up the locals of the calling frame as you did (such a horrible abuse of the poor language :)), there's no way to determine with any accuracy what exactly they decided to pass into you to get 'x'. I.e., I'm pretty sure its impossible to tell the difference between: x = 3 f(x) And: x = 3 f(3) From within f()'s perspective. This kind of thing gets asked a lot when people don't understand Python's name binding and object passing characteristics and come from other languages. It especially surprises people sometimes to learn that: x = 30000 f(x) f(30000) That in this case, 'f' will receive two completely different objects-- different id's and all. And yet: x = 3 f(x) f(3) In this case, each call will receive the exact same object. > 2. Is there a better way to loopup by id? I'm not very familiar with > sys.exc_info, but creating the id->name hash each time seems like > overkill. Do you mean given a certain 'id', look up what object it is? No there's no way to do that. An id in Python is just informational, and an implementation detail. The language doesn't provide any meaningful tools you can use with them. I mean there's gc.get_objects(), but that doesn't return everything. > 3. Is there a reference on all the special variables, like __foo__? http://docs.python.org/reference/datamodel.html > 4. Is there any work on deparsing (like Perl's deparse) lambda > functions to inline algebra and get a performance gain? There's nothing like that which I'm aware of. You can use projects like numpy or cython to get performance gain in important areas. -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
signature.asc
Description: OpenPGP digital signature
-- http://mail.python.org/mailman/listinfo/python-list