Hello all,
I'm hoping someone can tell me what the "self" is referring to in a Toolbox's Tool's method definitions. It's not what I expect it to be.
Below is an example with regards to a property set on the Tool object in its getParameterInfo method using "self". The following is the smallest example I could come up with to illustrate the behaviour which is unexpected to me. I can perform the following steps in both ArcPro 3.0.2 and ArcCatalog 10.4.1 with the same results, so this is not new. The code I provide at the end of this post will run in either product as is.
1. In ArcPro or ArcCatalog (or ArcMap, I assume), create a new Python Toolbox to generate a pyt file that includes default class definitions for Toolbox and Tool objects. The following code changes are all within the Tool class' definition.
2. Add a version property to Tool and set its value to 1;
3. In Tool's getParameterInfo(self) method, add an arcpy.Parameter to ensure its code is running and before returning the parameter, set self.version to 100;
4. In Tool's execute method, add an arcpy message to print out the value in self.version.
5. Run the tool. No need to make any changes in its GUI, just load it, run it and look at its output messages.
When I run this tool, the value that is output by the "print" message is 1. I expect it to be 100.
As a further test, I put "self.version = " different values in updateParameters and updateMessages and tried changing the default value in the GUI's Version input box to get them to run. Still, when I run the task, the output tells me the value in self.version is 1.
Finally, in Tool's __init__ method, I set self.version to 2. That line is in the code below also, but is currently commented out. Uncomment it to try it yourself. This time, the "print" output 2, so updating the self.version property in the __init__ method works as I expect but not when done in any other Tool method.
Can anyone explain why it is not outputing 100 in my original test above? What is "self" referring to in those the Tool class methods' parameter lists? Or this there something wrong with how I am tryign to use it?
Regards,
# -*- coding: utf-8 -*-
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 = "toolbox"
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
# ------------------------------------------
# Added and initialized this property
# ------------------------------------------
version = 1
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
# -----------------------------------------
# Added this line
# -----------------------------------------
# self.version = 2
def getParameterInfo(self):
"""Define parameter definitions"""
# ------------------------------------------
# This whole method is new
# ------------------------------------------
# params = None
# return params
version_param = arcpy.Parameter(
displayName="Version",
name="version",
datatype="GPString",
parameterType="Optional",
direction="Input")
version_param.value = "testing, testing, 123 ..."
self.version = 100
return [version_param]
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."""
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."""
# ------------------------------------------
# Added this line
# ------------------------------------------
arcpy.AddMessage("execute: " + str(self.version))
return
def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return