imported parameters disappear while running script

498
5
12-19-2011 08:42 AM
MaartenHofman1
New Contributor
I'm running a script for a toolbox, and I get the parameters for it using arcpy.GetParameterAsText().
It gets the parameters without problems, but in the second go of a while loop (without cursors), it seems that I cannot use the same arcpy.GetParameterAsText() to define a new variable. It just returns an empty string... what happened to the parameters I entered? Anyone has had similar problem?

Maarten
Tags (2)
0 Kudos
5 Replies
Luke_Pinner
MVP Regular Contributor
It is better practice to store the return value from a function/method in a variable if you are going to use it more than once rather than repeatedly calling the function method.

param1=arcpy.GetParameterAsText(1)
for i in range(10):
    if i >=param1: #eg use a variable instead of calling GetParameterAsText 10x
        dosomething()
0 Kudos
MaartenHofman1
New Contributor
Thanks for the tip and sample code...
I do store every return in a variable, but in the first go of the 'while' loop my script needs to reset one of the variables to an empty string, whereas it needs to have the original input for the second go.
I thought the passed arguments to arcpy.GetParameterAsText() would persist all through the script (I've tried it with shorter version of the script - including the while loop - and that seems to work).
Any more ideas?
0 Kudos
curtvprice
MVP Esteemed Contributor
Thanks for the tip and sample code...
I do store every return in a variable, but in the first go of the 'while' loop my script needs to reset one of the variables to an empty string, whereas it needs to have the original input for the second go.
I thought the passed arguments to arcpy.GetParameterAsText() would persist all through the script (I've tried it with shorter version of the script - including the while loop - and that seems to work).
Any more ideas?


It's possible that if you run another tool in the script that's going to reset the parameter vector. I agree that capturing the information in one section of your script - once - is much safer.

A thing to consider is whether there is something in your tool that lends it self to parameter validation code.
0 Kudos
Luke_Pinner
MVP Regular Contributor
Just make two copies of the parameter variable.
>>> param1a=param1b=arcpy.GetParameterAsText(1)
>>> print param1a
Some Parameter
>>> print param1b
Some Parameter
>>> print param1a==param1b
True
>>> param1a='Something else'
>>> print param1a==param1b
False
0 Kudos
MaartenHofman1
New Contributor
Ok, I figured that I could just define two variables from the same input, but it still strikes me as odd that it has to work like that... Anyways, thanks for your suggestions!
0 Kudos