Using a variable in Python


#1

I’m trying to use a variable value set in Panorama X’s scripting language, within a python command as follows:

local txtResult

txtResult = "Hello World"
message python(|||
print $«txtResult»$
|||)

This fails with the error:

PYTHON ERROR: Field or Variable [txtResult] does not exist.

If I change

$«txtResult»$

to

$«{txtResult}»$

that results in the output of “txtResult”.

How do I get the python script to output the value of the txtResult variable?


#2

From my quick experiments it seems the variable must be a fileglobal to work properly.


#3

Thank you for the quick response!

Would that be considered a bug? It would be nice if a local variable could be used, although cleaning up after a fileglobal isn’t that difficult.

Eric


#4

Seems to possibly be a bug in the python( function since it is not mentioned in the Help docs and local variables work fine when using the python statement.


#5

Local variables work only in the procedure they are defined in. They don’t work anywhere else.

There is one exception to this – the uselocalvariables statement allows a procedure to temporarily access the local variables of the procedure that called it. The python statement uses this trick to allow you it to access local variables in the code that called it.

When the python( function is used, however, your code is not the calling subroutine. Panorama goes thru quite a few complicated hoops to allow a procedure to be used in a formula, and because of going thru these hoops there is no way for a procedure to access the local variables in code up the chain (if there is any code up the chain, a formula could be used without any calling subroutine).

So, while I suppose this could be considered a bug, this behavior is not likely to ever change. If it did, it would be part of a huge refactor in how custom functions are handled, not as a specific fix to the python( function.

If you want to use local variables, use the python statement instead of the python( function. If you are using the python( function, you’ll need to use either fileglobal, windowglobal or global variables.

Should this included in the documentation for the python( function? Absolutely, and I have just now added this. To be honest, I wasn’t even aware of this issue until the subject came up in this post, so naturally I didn’t document it. This same issue will also affect the shellscript(, applescript(, perl(, ruby( and php( functions, and those help pages have also been updated. This pages won’t be updated in Panorama itself until the next release, but the online versions are already updated.


#6

Thank you, Jim for the excellent explanation. I suspected something along those lines and am very happy that the documentation has been updated.