3

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

Results Window

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 4, 2017 at 16:58
1
  • 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. Commented Dec 4, 2017 at 18:10

2 Answers 2

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.

Hornbydd
44.9k5 gold badges43 silver badges84 bronze badges
answered Dec 4, 2017 at 18:02
3
  • I have tried this and the values do not persist. Commented 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? Commented 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. Commented Dec 5, 2017 at 14:24
2

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
answered Dec 5, 2017 at 14:22
1
  • Who would have thought! Commented Dec 5, 2017 at 14:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.