<?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: python-toolbox: is it possible to define a function wihtin __init__ or execute? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1318074#M68386</link>
    <description>&lt;P&gt;Yes. But you need to understand scope.&amp;nbsp; At it's simplest, functions (and variables) are objects and objects are only "visible" at (or below) the scope in which they are created.&lt;/P&gt;&lt;P&gt;So by creating a function inside the local scope of a method (e.g.&amp;nbsp;__init__ or execute), you can only use that function&amp;nbsp;&lt;EM&gt;inside&lt;/EM&gt; the method you defined it in.&lt;/P&gt;&lt;P&gt;E.g.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class Tool(object):
    def __init__(self):
        def func_in_init():
            return "init"
    def execute(self):
        def func_in_exec():
            return "exec" 
        
        print(func_in_exec())  # this will work
        print(func_in_init())  # this will raise NameError
        &lt;/LI-CODE&gt;&lt;P&gt;If you want to use a function anywhere, define it globally, if you want to use it anywhere in your Tool class and not outside the class, make it a method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Aug 2023 22:32:12 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2023-08-11T22:32:12Z</dc:date>
    <item>
      <title>python-toolbox: is it possible to define a function wihtin __init__ or execute?</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1317414#M68371</link>
      <description>&lt;P&gt;is it possible to define a function wihtin __init__ or execute? if yes, how?&lt;/P&gt;&lt;P&gt;like this doesnot work:&lt;/P&gt;&lt;P&gt;class Toolbox(object):&lt;BR /&gt;def __init__(self):&lt;BR /&gt;self.label = "Toolbox"&lt;BR /&gt;self.alias = "toolbox"&lt;/P&gt;&lt;P&gt;self.tools = [Tool]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def myfunction():&lt;BR /&gt;[...]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;also this doesnot work:&lt;/P&gt;&lt;P&gt;def execute(self, parameters, messages):&lt;BR /&gt;def myfunction():&lt;BR /&gt;[...]&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2023 15:02:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1317414#M68371</guid>
      <dc:creator>nadja</dc:creator>
      <dc:date>2023-08-10T15:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: python-toolbox: is it possible to define a function wihtin __init__ or execute?</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1318070#M68385</link>
      <description>&lt;P&gt;I strongly suggest learning how Python classes are written, the &lt;A href="https://docs.python.org/3.9/tutorial/classes.html" target="_self"&gt;official docs&lt;/A&gt; are a good reference and there are many tutorials online that can walk you through creating your first class.&lt;/P&gt;&lt;P&gt;That said, there's nothing stopping you from defining functions within another function or method and then calling them. The catch is functions are bound to the scope they're defined in, which means you can't call an inner function outside of the method/function/etc. it's defined within.&lt;/P&gt;&lt;P&gt;The most likely reason your first example is failing is because you defined a method with no "self" parameter or no "@staticmethod" decorator, which makes it useless. Instance methods need self as the first parameter, decorated static methods do not.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 22:26:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1318070#M68385</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2023-08-11T22:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: python-toolbox: is it possible to define a function wihtin __init__ or execute?</title>
      <link>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1318074#M68386</link>
      <description>&lt;P&gt;Yes. But you need to understand scope.&amp;nbsp; At it's simplest, functions (and variables) are objects and objects are only "visible" at (or below) the scope in which they are created.&lt;/P&gt;&lt;P&gt;So by creating a function inside the local scope of a method (e.g.&amp;nbsp;__init__ or execute), you can only use that function&amp;nbsp;&lt;EM&gt;inside&lt;/EM&gt; the method you defined it in.&lt;/P&gt;&lt;P&gt;E.g.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;class Tool(object):
    def __init__(self):
        def func_in_init():
            return "init"
    def execute(self):
        def func_in_exec():
            return "exec" 
        
        print(func_in_exec())  # this will work
        print(func_in_init())  # this will raise NameError
        &lt;/LI-CODE&gt;&lt;P&gt;If you want to use a function anywhere, define it globally, if you want to use it anywhere in your Tool class and not outside the class, make it a method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 22:32:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-toolbox-is-it-possible-to-define-a-function/m-p/1318074#M68386</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2023-08-11T22:32:12Z</dc:date>
    </item>
  </channel>
</rss>

