Select to view content in your preferred language

Make Python Toolbox parameters a namedtuple instead of a list

1495
3
01-13-2022 08:53 PM
Status: Implemented
Labels (1)
Luke_Pinner
MVP Regular Contributor

ArcGIS Pro passes parameters to Python Toolbox (.pyt) Tool updateParameters, updateMessages and execute methods as a list.  Changing this to a namedtuple by default would allow access by parameter name as well as by index.

Currently users can create a dictionary or a namedtuple themselves, e.g the way I do it is below, but I've also seen some fairly awkward attempts with globals and loops. 

As a dict:

 

class Tool
    etc...
    
    def execute(self, parameters, messages):
        parameters = {p.name: p for p in parameters}
        # do something with parameters["some_name"]

 

 

As a namedtuple:

 

from collections import namedtuple

class Tool
    etc...

    def execute(self, parameters, messages):
        parameters = namedtuple('Parameters', (p.name for p in parameters))(*parameters)
        # do something with parameters[0] or parameters.some_name

 

 

 

3 Comments
JohannesLindner

This would be great.

  • parameters[1] is just so non-descriptive as opposed to parameters.InTable or parameters["InTable"]
  • using indices is just suicide when you are still adding/removing/moving parameters

I use the dictionary approach in every one of my tools and it's not too much hassle, but it could be completely avoided if parameters was a named tuple from the start. That would also save me from having to create the dict multiple times in a tool (updateParameters(), updateMessages() and execute())

AJR
by

This would be super useful.

HannesZiegler
Status changed to: Implemented

The following functions and classes now support name-based parameter referencing:

Please see the What's New documentation for more new features in Pro 3.2. Additionally, we have also posted a Your Ideas in ArcGIS Pro 3.2 blog and video, take a look if interested!