How do I create a global variable in a python toolbox?
The eventual purpose is that I would like to store information related to the execution of my script across functions, but not as "parameters". They might be boolean, or dictionaries, but in my example below, I am using a "String for simplicity.
Add text to the Something parameter, move to Something Else, and click Ok. On execution, the value of self.whatever is added to the messages in ArcCatalog. It's the initial value of "ABC".
import arcpy
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
self.whatever = "ABC"
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Something",
name="something",
datatype="GPString",
parameterType="Required",
direction="Input"
)
params = [param0]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
self.whatever = "XYZ"
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
arcpy.AddMessage("The value whatever is: " + self.whatever)
return
-
This entry in the Python programming FAQ might be helpful: docs.python.org/3/faq/…. If you're looking for cross-toolbox data support in ArcPy, I'm unable to help.sgillies– sgillies2017年12月04日 18:10:20 +00:00Commented Dec 4, 2017 at 18:10
2 Answers 2
Just write:
import arcpy
aGlobal = None
class aClass():
aMethod(self):
global aGlobal
aGlobal = self.whatever
Without the global
statement it will be local inside the method. Alternatively you can set a non-self variable inside the class, which I think survives the object life cycle.
-
I have tried this and the values do not persist.Liz Eidsness– Liz Eidsness2017年12月05日 13:53:10 +00:00Commented Dec 5, 2017 at 13:53
-
Hm, seems that global isn't the problem at all. I added a arcpy.AddMessage("The value whatever is: " + self.whatever) in updateParameters() but this method isn't called, the message does not show in the results window. So, for me, the question arises what are you trying to achieve?Andreas Müller– Andreas Müller2017年12月05日 14:14:40 +00:00Commented Dec 5, 2017 at 14:14
-
I think the problem is that the python toolbox is wonky and doesn't behave in the way one would expect. I've noticed too that AddMessage doesn't do anything until execute() is called. I've had to use logging to a file for debugging. I stripped out all of that for simplicity here.Liz Eidsness– Liz Eidsness2017年12月05日 14:24:06 +00:00Commented Dec 5, 2017 at 14:24
I found an answer on another post.
[Python toolbox tool objects do not remember instance variables between function calls?
The behaviour of the Python Toolbox is wonky and not following what one would expect. Multiple instances of the tool class are created. Thus negating everything I set. Apparently getParameterInfo is only called once, so it's where I create the global. Setting it in updateParameters then works, and output is as expected.
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Toolbox"
self.alias = ""
self.tools = [Tool]
class Tool(object):
def __init__(self):
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
global junk
junk = None
param0 = arcpy.Parameter(
displayName="Something",
name="something",
datatype="GPString",
parameterType="Required",
direction="Input"
)
params = [param0]
return params
def isLicensed(self):
return True
def updateParameters(self, parameters):
global junk
junk = "XYZ"
return
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
global junk
arcpy.AddMessage("The value junk is: " + junk)
return
-
Who would have thought!Andreas Müller– Andreas Müller2017年12月05日 14:58:43 +00:00Commented Dec 5, 2017 at 14:58