How to call a function in a function in python
<p>I have a problem about function in python, and i'm pretty new in programming, so please giving your examples in the simplest way, thanks</p>

<p>I want to call a function in a function like this:</p>

<pre><code>def func1 (a):
    b = a + 1
    def func2():
       c = b*3
       print c
    def func3():
       c = b/5
       print c

func1(5).func2()
</code></pre>

<p>how can i call func2 inside func1 or func3 inside func1 ? and yes, i do need func1, func2 and func3, i don't want to use class</p>

<p>The function objects <code>func2</code> and <code>func3</code> are local to the function <code>func1</code>, which means they do not exist outside of <code>func1</code> unless <code>func1</code> explicitly returns them to the calling environment. </p>

<p>So if you want to call those functions <em>outside</em> of <code>func1</code> (as your posted code attempts to do) you need to do something like this:</p>

<pre><code>def func1(a):
    b = a + 1
    def func2():
       c = b*3
       print c
    def func3():
       c = b/5
       print c
    return func2, func3

f2, f3 = func1(5.0)
f2()
f3()
</code></pre>

<p><strong>output</strong></p>

<pre><code>18.0
1.2
</code></pre>

<p><code>f2</code> and <code>f3</code> will use the value of the <code>a</code> parameter that was given to <code>func1</code> when they were created. If you want to use a different <code>a</code> then simply call <code>func1</code> again; each time you call it new instances of the  <code>func2</code> and <code>func3</code> function objects are created.</p>

<p><hr>
<em>Aside</em></p>

<p>Note that I passed an argument of <code>5.0</code> rather than <code>5</code> to <code>func1</code>. That's to ensure that <code>b/5</code> in <code>func3</code> uses float division. That's not necessary in Python 3, and to get the Python 3 behaviour in Python 2 you can put</p>

<pre><code>from __future__ import division
</code></pre>

<p>at the top of your script.</p>

python function nested