473,779 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nested functions

I often make helper functions nested, like this:

def f():
def helper():
...
...

is it a good practice or not? What about performance of such
constructs?

--
Regards, Gregory.

Jun 14 '06 #1
9 2853
"Gregory Petrosyan" <gr************ ***@gmail.com> writes:
I often make helper functions nested, like this:

def f():
def helper():
...
...

is it a good practice or not?
You have my blessing. Used well, it makes for more readable code.
What about performance of such constructs?


What about it? Set up some examples maningful for your situation, with
and without such constructs, and use the profiler to find out.

--
\ "People demand freedom of speech to make up for the freedom of |
`\ thought which they avoid." -- Soren Aabye Kierkegaard |
_o__) (1813-1855) |
Ben Finney

Jun 15 '06 #2
Ben Finney wrote:
"Gregory Petrosyan" <gr************ ***@gmail.com> writes:
I often make helper functions nested, like this:

def f():
def helper():
...
...

is it a good practice or not?


You have my blessing. Used well, it makes for more readable code.


I'm not sure it's in general more readable; I typically use nested
functions for closures only, not helper functions, so I'd read the code
twice to check if it's a closure and if not why might have been defined
locally. I prefer to define helpers at the module level, often making
them 'private' by prepending their name with a single underscore.
What about performance of such constructs?


What about it? Set up some examples maningful for your situation, with
and without such constructs, and use the profiler to find out.


It shouldn't come as a surprise if it turns out to be slower, since the
nested function is redefined every time the outer is called. If you
actually call the outer function a lot, you'd better profile it.

George

Jun 15 '06 #3
George Sakkis wrote:
Ben Finney wrote:
"Gregory Petrosyan" <gr************ ***@gmail.com> writes:
> I often make helper functions nested, like this:
>
> def f():
> def helper():
> ...
> ...
>
> is it a good practice or not?


You have my blessing. Used well, it makes for more readable code.


I'm not sure it's in general more readable; I typically use nested
functions for closures only, not helper functions, so I'd read the code
twice to check if it's a closure and if not why might have been defined
locally. I prefer to define helpers at the module level, often making
them 'private' by prepending their name with a single underscore.
> What about performance of such constructs?


What about it? Set up some examples maningful for your situation, with
and without such constructs, and use the profiler to find out.


It shouldn't come as a surprise if it turns out to be slower, since the
nested function is redefined every time the outer is called.


That's right. However, if the outer function is only called a few times
and the nested function is called a lot, the locals lookup for the
function name is theoretically faster than the globals lookup. Also,
in methods you can use closures, so you don't have to pass, for example,
self to the inner function.

Georg
Jun 15 '06 #4
Georg Brandl wrote:
That's right. However, if the outer function is only called a few times
and the nested function is called a lot, the locals lookup for the
function name is theoretically faster than the globals lookup. Also,
in methods you can use closures, so you don't have to pass, for example,
self to the inner function.


If you are worried about the overhead of looking up the function name in
the local rather than global scope then you should also worry about the
overhead of accessing self through a closure rather than as a parameter.

As always in these cases, don't worry about it until you know definitely
(by timing) that performance is an issue in that part of your code, and
then time the different options and refuse the temptation to guess as you
will probably get it wrong. The relative times here will depend on a lot of
factors, such as how often you access the closure/parameter, and whether or
not there are other arguments to the function.

I frequently nest functions, but I do it in cases where I want to simplify
a function body and don't see any case for creating yet another generally
accessible method or function.

Some benefits of nested functions: you can use a function name which is
short by self-explanatory within the context of the outer function without
having to worry about it conflicting with other function/variable names.

The extracted functions are kept close to the place where they are used: a
small support function which is a few hundred lines away from where it
is used is more accident prone than one right next to the place it is used.
Also, if you later refactor out the main method the support functions will
disappear as well rather than lying around unused.

You can use closures, not because you have to, but because it simplifies
the calls and therefore keeps expressions simpler and easier to read.

Of course you can also mess things up totally by overdoing it.
Jun 15 '06 #5
George Sakkis wrote:
It shouldn't come as a surprise if it turns out to be slower, since the
nested function is redefined every time the outer is called.


except that it isn't, really: all that happens is that a new function object is created from
prebuilt parts, and assigned to a local variable. it's not slower than, say, a method call.

</F>

Jun 15 '06 #6
Fredrik Lundh wrote:
George Sakkis wrote:
It shouldn't come as a surprise if it turns out to be slower, since
the nested function is redefined every time the outer is called.


except that it isn't, really: all that happens is that a new function
object is created from prebuilt parts, and assigned to a local
variable. it's not slower than, say, a method call.

It looks to be somewhat faster than a method call:

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testMethod(t .instance,
42)"
1000 loops, best of 3: 1.58 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testMethod2( t.instance,
42)"
100 loops, best of 3: 1.61 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested(t .instance,
42)"
1000 loops, best of 3: 1.06 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested2( t.instance,
42)"
1000 loops, best of 3: 1.08 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested3( t.instance,
42)"
1000 loops, best of 3: 1.13 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested4( t.instance,
42)"
1000 loops, best of 3: 1.23 msec per loop
--------- t.py -------------
class C:
def m1(self):
return 42

def m2(self, x):
return x

instance = C()

def testMethod(inst ance,x):
n = 0
while n < 100000:
n += instance.m1()

def testMethod2(ins tance, x):
n = 0
while n < 100000:
n += instance.m2(x)

def testNested(inst ance, x):
def m1():
return 42
n = 0
while n < 100000:
n += m1()

def testNested2(ins tance, x):
def m2():
return x
n = 0
while n < 100000:
n += m2()

def testNested3(ins tance, x):
def m2(y):
return y
n = 0
while n < 100000:
n += m2(x)

def testNested4(ins tance, x):
def m2(y):
return x
n = 0
while n < 100000:
n += m2(x)

----------------------------

The differences between the nested function calls show how difficult it can
be guessing what will be faster: #3&#4 show that all, else being equal,
accessing the closure is much slower than accessing a parameter, but #2
shows that not passing any parameters to the nested function more than
compensates for the single slow closure access.
Jun 15 '06 #7
Duncan Booth wrote:
Fredrik Lundh wrote:
George Sakkis wrote:
It shouldn't come as a surprise if it turns out to be slower, since
the nested function is redefined every time the outer is called.


except that it isn't, really: all that happens is that a new function
object is created from prebuilt parts, and assigned to a local
variable. it's not slower than, say, a method call.

It looks to be somewhat faster than a method call:

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testMethod(t .instance,
42)"
1000 loops, best of 3: 1.58 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testMethod2( t.instance,
42)"
100 loops, best of 3: 1.61 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested(t .instance,
42)"
1000 loops, best of 3: 1.06 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested2( t.instance,
42)"
1000 loops, best of 3: 1.08 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested3( t.instance,
42)"
1000 loops, best of 3: 1.13 msec per loop

C:\temp>\python 24\lib\timeit.p y -s "import t" "t.testNested4( t.instance,
42)"
1000 loops, best of 3: 1.23 msec per loop
--------- t.py -------------
class C:
def m1(self):
return 42

def m2(self, x):
return x

instance = C()

def testMethod(inst ance,x):
n = 0
while n < 100000:
n += instance.m1()

def testMethod2(ins tance, x):
n = 0
while n < 100000:
n += instance.m2(x)

def testNested(inst ance, x):
def m1():
return 42
n = 0
while n < 100000:
n += m1()

def testNested2(ins tance, x):
def m2():
return x
n = 0
while n < 100000:
n += m2()

def testNested3(ins tance, x):
def m2(y):
return y
n = 0
while n < 100000:
n += m2(x)

def testNested4(ins tance, x):
def m2(y):
return x
n = 0
while n < 100000:
n += m2(x)

----------------------------

The differences between the nested function calls show how difficult it can
be guessing what will be faster: #3&#4 show that all, else being equal,
accessing the closure is much slower than accessing a parameter, but #2
shows that not passing any parameters to the nested function more than
compensates for the single slow closure access.


It would also be interesting to add unnested versions of m1(), m2()
(functions, not methods) to the comparison.

George

Jun 15 '06 #8
Thanks everybody for your help!

Jun 15 '06 #9
Fredrik Lundh wrote:
George Sakkis wrote:
It shouldn't come as a surprise if it turns out to be slower, since the
nested function is redefined every time the outer is called.


except that it isn't, really: all that happens is that a new function object is created from
prebuilt parts, and assigned to a local variable. it's not slower than, say, a method call.


Interesting. So func_code for a nested function is created when the
module is compiled, and stuck in a new function object when the
definition is executed. Like George, I always assumed that the body of
the nested function was compiled when the outer function was executed,
but that doesn't really make any sense - the *code* for the inner
function is static, just the environment changes (globals(), closure).

dis.dis reveals all:

In [10]: def g():
....: def h():
....: print 'foo'
....: return h
....:

In [11]: dis.dis(g)
2 0 LOAD_CONST 1 (<code object h at 00E8D960,
file "<ipython console>", line 2>)
3 MAKE_FUNCTION 0
6 STORE_FAST 0 (h)

4 9 LOAD_FAST 0 (h)
12 RETURN_VALUE

Thanks Fredrik!
Kent
Jun 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2344
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
2569
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
6
12204
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method prototype in another prototype but seems pointless. Can someone please write a short, simple, and concise skeleton code of how to use nested functions? class Foo {
2
7513
by: Forgone Conclusion | last post by:
Hi, I have a View that groups and sums up totals. This View is then nested within in another View and used (it needs to be done like this). What i need to do is to be able to vary the records in the nested query by specifying dates. These would somehow need to be passed to the nested query. I've looked into stored procedures/functions but am still stumped on
7
2253
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
10
3246
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
4
2315
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one could implement an equivalent behaviour with plain C (in this case I'm thinking of the language I'm developing, which shall be converted into C for target compilation). So far I didn't touch the topic "nested functions", since I just don't see an...
2
1666
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent and its nested children all in one module, declare the children static - voila. Std-C. Regards, Johannes
9
3991
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback (most recent call last):
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.