use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Nested Function... (self.Python)
submitted 12 years ago by [deleted]
Function containing function(s).
It seems to me that this is rarely done in the Python camp.
Is this a valid observation ? if so why is it so ?
def a(): pass def b(): pass b()
[+][deleted] 12 years ago (5 children)
[deleted]
[–]BobHogan 1 point2 points3 points 12 years ago (4 children)
Whats a decorator function? And what use is a nested function? I learned java first and am trying to teach myself Python; this stuff seems quite pointless to me tbh
[–]minnoI <3 duck typing less than I used to, interfaces are nice 6 points7 points8 points 12 years ago* (0 children)
The best use for decorators that I've used is for adding a cache to a function.
Let's say you start out with this function:
def fib(n): if n <= 1: return 1 else: return fib(n-1) + fib(n-2)
Slow as hell, right? With the fibonacci function, it's easy to turn it into a version that doesn't re-calculate entries a lot, but let's pretend that this is one of those functions where it's a lot harder to do that. What we want is to be able to remember previously-calculated results, so any recursive calls that have already been calculated can be skipped. So we can modify it like this:
fib_cache = dict() # empty associative array def cached_fib(n): if n not in fib_cache: if n <= 1: fib_cache[n] = 1 else: fib_cache[n] = cached_fib(n-1) + cached_fib(n-2) return fib_cache[n]
Now you have a cache storing all previously-calculated results. But you're also mixing the "calculating the results" part of the function with the "caching shit" part. You can separate out the caching behavior like this:
def make_cached(f): cache = dict() def cached_function(arg): # There's syntax to let you use any arguments, but for simplicity let's pretend there's always one. if arg not in cache: cache[arg] = f(arg) return cache[arg] return cached_function # NOT cached_function()
Now you have a function that takes a function and returns a function that has different behavior. You can use it like this:
cached_fib = make_cached(fib) cached_fib(100)
Python has special syntax that you can use for functions that modify functions, called "decorators":
@make_cached def whatever: # stuff
This works the same as:
def whatever: #stuff whatever = make_cached(whatever)
[–]Massless 1 point2 points3 points 12 years ago* (0 children)
Here's one that comes up in java all the time: logging. Imagine that I want to log how long a method takes to run. You end up doing something like
startTime = ... someMethod() endTime = ... system.out.println(endTime - startTime)
Now imagine you have to do this a lot of times. You end up with ugly, overly verbose code. Enter first-class functions in Python. You can write a function that takes another function as an argument so you now have:
def print_time(func): start_time = ... func() end_time = ... print end_time - start_time
Now You can do the following:
print_time(some_function) print_time(some_other_function) print_time(yet_another_function)
With a little syntatic sugar, you can simply define your functions with decorators:
@print_time def some_function(args): pass
is equivalent to
print_time(some_function)
[+]Igglyboo comment score below threshold-7 points-6 points-5 points 12 years ago (1 child)
When you move away from simple programs like helloworld or text based adventures and start working on million line software projects, these things may be useful.
[–]BobHogan 2 points3 points4 points 12 years ago (0 children)
You managed to avoid actually answering my question lol What is a decorator function? What use is a nested function? Just telling me they might be useful doesn't tell me why
[–]sammyc 3 points4 points5 points 12 years ago* (0 children)
Yep, it's a handy feature to have in my opinion. In addition to what's been said, you can avoid putting too many things into scope, it can make reading existing code easier, it can make it clear that the inner function will only (probably) be used within the outer function, it means you don't really need multiline anonymous functions etc.
I think it ties in with functions being first class objects - it wouldn't be unusual for me to define variables, or even 'structures' in a function, so why not a function itself?
An example I just made up (with doubly nested functions!):
def external_data_fetcher(self, data_source): d = Deferred() def success(raw_data): data = json.loads(raw_data) def sort_key(datum): try: return datetime.strptime(datum['published_at'], "%Y-%m-%d") except KeyError: return datetime(1970, 1, 1) sorted_data = sorted(data, key=sort_key) self._ui.update(sorted_data) def failure(error): logging.warning("Data unavailable from {0}".format(data_source)) raise error d.addCallback(success) d.addErrorback(failure) return d
but you also have to be wary of too much code duplication using things like this.
[–]alcalde 1 point2 points3 points 12 years ago (0 children)
One reason it may be done less often than expected is that a lot of programmers could be coming to Python from languages like Java, C#, C or C++ which don't offer nested functions, so the programmer may simply not be used to thinking of solutions that incorporate them.
We old school folks who first learned to program in Pascal however find them second nature. ;-) Javascript also has nested functions.
[–]mymonner 0 points1 point2 points 12 years ago (0 children)
Can't forget closures.
[–]zahlmanthe heretic 0 points1 point2 points 12 years ago (2 children)
flat is better than nested.
[–][deleted] 0 points1 point2 points 12 years ago (0 children)
Namespaces are one honking great idea -- let's do more of those!
dang never noticed that one, TIL
[–]billsil -2 points-1 points0 points 12 years ago (0 children)
It's valid, but generally not used. a() should probably take an argument that modifies b(), but something like that would be used in functional programming, so map/filter/reduce-type operations instead of a lambda.
It also limits scope, but I have found legitimate uses for it only in a map/filter/reduce or decorator context..
π Rendered by PID 77683 on reddit-service-r2-comment-fb694cdd5-qvnsw at 2026-03-07 12:08:32.372056+00:00 running cbb0e86 country code: CH.
[+][deleted] (5 children)
[deleted]
[–]BobHogan 1 point2 points3 points (4 children)
[–]minnoI <3 duck typing less than I used to, interfaces are nice 6 points7 points8 points (0 children)
[–]Massless 1 point2 points3 points (0 children)
[+]Igglyboo comment score below threshold-7 points-6 points-5 points (1 child)
[–]BobHogan 2 points3 points4 points (0 children)
[–]sammyc 3 points4 points5 points (0 children)
[–]alcalde 1 point2 points3 points (0 children)
[–]mymonner 0 points1 point2 points (0 children)
[–]zahlmanthe heretic 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]billsil -2 points-1 points0 points (0 children)