<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Best practice for dealing with Optional Parameters in Script Tools? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111468#M62807</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Something like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Optionalparam = arcpy.getparam…(1)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;if Optionalparam = “” or Optionalparam is None:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; Optionalparam = None&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;sorry, I’m on my phone and it doesn’t do the code syntax window very well. I’ll fix this tonight.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Oct 2021 21:56:29 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2021-10-26T21:56:29Z</dc:date>
    <item>
      <title>Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111378#M62791</link>
      <description>&lt;P&gt;Here's a crude example to at least put forth some effort in asking this question. I tried searching and got some results but I fear my inquiry is too simplistic to find viable results.&lt;/P&gt;&lt;P&gt;Let's say we have just two parameters defined in the code; and in the script tool we have configured parameter 1 to be 'required' and parameter 2 to be 'optional'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;RequiredParameter = arcpy.GetParameterAsText(0)
OptionalParameter = arcpy.GetParameterAsText(1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So if your code has a line expecting OptionalParameter, the script will fail. So how best to evaluate it being supplied by the user or not?&lt;/P&gt;&lt;P&gt;A few options I have found:&lt;/P&gt;&lt;P&gt;Evaluate for presence of the optional parameter:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if OptionalParameter and OptionalParameter != "#":
    #Do something with optional parameter&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try-Except block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    #arcpy tool using the supplied optional parameter
except:
    pass #move on with life never knowing if anything went wrong or why it did&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I suppose something could be done within the script tool validation, but the code itself would have to be modified as well.&lt;/P&gt;&lt;P&gt;Pitfalls of either? Better options exist?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 18:49:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111378#M62791</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-26T18:49:48Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111451#M62802</link>
      <description>&lt;P&gt;I would say you should find a way to use a default value if&amp;nbsp;OptionalParameter is not defined and write your code logic in a way that can handle any valid value (including the default).&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:21:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111451#M62802</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-26T21:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111455#M62804</link>
      <description>&lt;P&gt;I was thinking something similar but don't know how to implement it exactly. But the trick here is that if a user supplies no value, that is actually valid because they don't want to employ this parameter and this is the most likely scenario. Follow me?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:30:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111455#M62804</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-26T21:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111458#M62805</link>
      <description>&lt;P&gt;So keep it assigned to none and in your code where you need that parameter, split your process to execute either option.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;if optionalparameter is not none:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Use it&lt;/P&gt;&lt;P&gt;else:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Don’t use it.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:47:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111458#M62805</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-26T21:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111460#M62806</link>
      <description>&lt;P&gt;Ok I see where you are going!&lt;/P&gt;&lt;P&gt;I've never set a default to none. Is none a valid input for default&amp;nbsp; or something like "" needs to be set?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:50:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111460#M62806</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-26T21:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111468#M62807</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Something like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Optionalparam = arcpy.getparam…(1)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;if Optionalparam = “” or Optionalparam is None:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; Optionalparam = None&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;sorry, I’m on my phone and it doesn’t do the code syntax window very well. I’ll fix this tonight.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 21:56:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111468#M62807</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-26T21:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111470#M62808</link>
      <description>&lt;P&gt;Hah! So am I, many thanks! What ok curious about is exactly how to set the default to none in the script tool parameter dialogue...I can't believe I've never done that before...&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 22:10:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111470#M62808</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-26T22:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111471#M62809</link>
      <description>&lt;P&gt;Just leave it blank and it will pass None if nothing is entered through the tool UI.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 22:18:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111471#M62809</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-26T22:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111479#M62810</link>
      <description>&lt;P&gt;Ok,&amp;nbsp; I use this style for testing if the parameters from the tool's UI has been set, or set it to a default- (very useful for maintaining the scripts that are meant for standalone execution and as a script tool). Though, you may not even need it since it will either be a value or None and the conditional later on in your code would determine the course of action, but it is explicitly setting the variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;isOptional = arcpy.GetParameterAsText(1)
if isOptional == '' or isOptional is None:
    isOptional = None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 22:53:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111479#M62810</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-26T22:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111482#M62811</link>
      <description>&lt;P&gt;I'll just toss out that you could also simply check for &lt;A href="https://docs.python.org/3/library/stdtypes.html#truth-value-testing" target="_self"&gt;"truthiness"&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if isOptional:
    # isOptional has a value.
    # do a thing.
else:
    # isOptional has no value.
    # handle it.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 22:58:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111482#M62811</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-26T22:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111493#M62812</link>
      <description>&lt;P&gt;I prefer `named` parameters rather than positional parameters, particularly for those that are optional.&lt;/P&gt;&lt;P&gt;More of a notation, than a solution, but assume the `funk` is a script associated with a tool.&amp;nbsp; I have omitted error checking for the first two parameters to ensure that they have values (type hints can be used but that is another story.&lt;/P&gt;&lt;P&gt;So assume, that I pass the tool the tool 4 parameters (use GetParameter or GetParameterAsText if you want to check for None as 'None')&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def funk(req1, req2, **kwargs):
    """kwargs are optional named parameters.  I prefer `named` parameters."""
    opt1 = kwargs.get('opt1', None)
    opt2 = kwargs.get('opt2', None)
    args = [req1, req2, opt1, opt2]
    return  args
    

out = funk(1, 2, opt2=3)

print(out)
[1, 2, None, 3]&lt;/LI-CODE&gt;&lt;P&gt;note that the optional parameter (opt1) is checked for its existence (aka, it was named in the call to the script).&lt;/P&gt;&lt;P&gt;In `custom` python toolboxes you can set default values for your parameters like "None", "nothing to see here", "zilch".... whatever, then test for it in your scripts.&amp;nbsp; Avoid blanks or "" or '' because they all look the same, put in a default "null/ignore me" value.&amp;nbsp; You will thanks yourself in a year or two when you have to figure out what you did&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 00:00:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111493#M62812</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-10-27T00:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111600#M62815</link>
      <description>&lt;P&gt;Can anyone attest to the veracity of this statement here?&lt;/P&gt;&lt;P&gt;"The hash sign ("#") string is what the geoprocessing framework passes in for unspecified values so you have to look for that in addition to the empty/null string case."&lt;/P&gt;&lt;P&gt;&lt;A href="https://gis.stackexchange.com/questions/53798/can-input-parameter-be-optional-and-python-script-tool-work-when-input-is-not-pr" target="_blank" rel="noopener"&gt;stack exchange link&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I ask because I noticed no one suggesting testing for != "#"&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 13:11:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111600#M62815</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-27T13:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111661#M62819</link>
      <description>&lt;P&gt;Maybe it was a thing back in '13 but I haven't seen any parameter come through as '#' so could be outdated? &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 15:08:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111661#M62819</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-27T15:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111662#M62820</link>
      <description>&lt;P&gt;I've seen some of the Esri GP tools do this. When you run the tool and then copy the Python snippet, it'll put "#" for the optional parameters that were left empty. I have never done this explicitly with my Python toolboxes and I don't see a need for it.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 15:11:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111662#M62820</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-10-27T15:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for dealing with Optional Parameters in Script Tools?</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111667#M62821</link>
      <description>&lt;P&gt;Thanks everyone I really learned a lot from this!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 15:16:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-for-dealing-with-optional-parameters/m-p/1111667#M62821</guid>
      <dc:creator>ZacharyHart</dc:creator>
      <dc:date>2021-10-27T15:16:45Z</dc:date>
    </item>
  </channel>
</rss>

