def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def MyFunction(inFeatureLayer): arcpy.SelectLayerByAttribute_management(inFeatureLayer, "NEW_SELECTION", '"ID" = ' + "'123456'") SiteList = list(r[0] for r in arcpy.da.SearchCursor(inFeatureLayer, "ID")) return SiteList def execute(self, parameters, messages): """The source code of the tool""" inFeatureLayer = arcpy.parameters[0].valueAsText Result = MyFunction(inFeatureLayer) # Why doesn???t this work??? # Do something with the result
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""" def MyFunction(inFeatureLayer): arcpy.SelectLayerByAttribute_management(inFeatureLayer, "NEW_SELECTION", '"ID" = ' + "'123456'") SiteList = list(r[0] for r in arcpy.da.SearchCursor(inFeatureLayer, "ID") return SiteList inFeatureLayer = arcpy.parameters[0].valueAsText Result = MyFunction(inFeatureLayer) # This works, but it seems more elegant to me if I could define it outside of Execute # Do something with the result
Solved! Go to Solution.
Result = self.MyFunction(inFeatureLayer)
You are trying to reference it at the class level without using self which won't work. You need to pass self to the function so the class can reference it, declare it within another function, or reference it outside the class.
MyFunction(self, param1, param2, param3): # Do stuff
Yes that should allow you to reference it within the class using self.
class Toolbox(object): def __init__(self): """Toolbox Initialization Settings""" self.label = "My Very Own Toolbox" self.alias = MVO_tbx" # List of the tools contained within the Toolbox self.tools = [ToolClass] class ToolClass(object): def __init__(self): """Define the tool.""" self.label = "MyTool" self.description = "No Description" self.canRunInBackground = True def getParameterInfo(self): """Establish the Parameters""" # Input Features Parameter param0 = arcpy.Parameter( displayName="Features", name="in_Features", datatype="GPFeatureLayer", parameterType="Required", direction="Input") param0.filter.list = ["Point"] 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 MyFunction(self, inFeatureLayer): arcpy.SelectLayerByAttribute_management(inFeatureLayer, "NEW_SELECTION", '"ID" = ' + "'123456'") SiteList = list(r[0] for r in arcpy.da.SearchCursor(inFeatureLayer, "ID")) return SiteList def execute(self, parameters, messages): """The source code of the tool""" inFeatureLayer = arcpy.parameters[0].valueAsText Result = MyFunction(inFeatureLayer) # Why doesn�??t this work??? # Do something with the result
TypeError: unbound method �??MyFunction�?� must be called with �??ToolClass�?� instance as first argument (got Layer instead)
def execute(self, parameters, messages): """The source code of the tool""" inFeatureLayer = arcpy.parameters[0].valueAsText Result = ToolClass.MyFunction(inFeatureLayer) # Why doesn�??t this work??? # Do something with the result
Result = self.MyFunction(inFeatureLayer)
You need to reference it using self. ToolClass is the name of your class and self is how the class is referenced when you are within that class.Result = self.MyFunction(inFeatureLayer)
I don't think there is anything in the .pyt format that would prevent this functionality but I have not tested this myself.