On Sun, Jun 07, 2009 at 07:53:34AM -0400, Alan Bromborsky wrote:
> What is the best way for a module to test to see if a particular symbol
> (in my case 't') has been created in the main program? I don't want a
> function to differentiate with respect to time if 't' has not been created.
In general, this is impossible and should not be attempted: a function
has access to two scopes, locals and globals, and should restrict itself
to these scopes.
Now with Python you can do crazy things, and I am quite confident you can
hack this behavior... For instance using tracebacks to inspect the frame
stack:
import sys
class MySpecialException(Exception):
pass
x = 1
def f():
try:
raise MySpecialException()
except MySpecialException, e:
_, _, t = sys.exc_info()
# Now print the n+1 local scope:
print t.tb_frame.f_back.f_locals
f()
:). I am not too sure what to think about such code, though. I tend to
think that you shouldn't rely on this kind of behavior, it makes your
code very impredictable from an user's point of view: there are very
subtles side effects.
Gaƫl
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---