Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Firstly let me clarify that I know C and am learning Python. So my OOPS is kind of bad.

I was reading the official tutorial and found this

Although scopes are determined statically, they are used dynamically. At any time during execution, there are at least three nested scopes whose namespaces are directly accessible:

  • the innermost scope, which is searched first, contains the local names
  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
  • the next-to-last scope contains the current module’s global names the outermost scope (searched last) is the namespace containing built-in names

I understand namespaces. I think scopes are the same thing. But I couldn't figure out what does the sentence about scopes mean? What is the advantage of such an arrangement?

I understand the sentence but couldn't visualize that. So please don't say that this is problem with my English.

share|improve this question
Scopes are rather different than namespaces. C does not have namespaces, but it does have scopes. C only has local and global scope, Python has some more, most importantly the outer function scope (C does not have nested functions, so there can't be any outer function). – Jan Hudec Jul 23 '13 at 9:27
Note, that this has absolutely nothing to do with object oriented. Closures (inner functions keeping access to the outer function variables) are functional programming concept. – Jan Hudec Jul 23 '13 at 9:28
up vote 5 down vote accepted

It lets you pass around those functions which use names from the surrounding context in their behaviour.

You see this a lot when defining decorators:

def make_bold(func):
    def wrapper(*args, **kw):
        return '<b>{}</b>'.format(func(*args, **kw))
    return wrapper

def hello(name):
    return 'Hello {}!'.format(name)

hello('World')  # returns '<b>Hello World!</b>'

Here the wrapper function accesses func from the parent function scope; func is a local variable in the make_bold function. wrapper is a closure; wrapper closes over func.

You can expand on this a little more by making the decorator configurable:

def format(tag):
    def decorator(func):
        def wrapper(*args, **kw):
            return '<{0}>{1}</{0}>'.format(tag, func(*args, **kw))
        return wrapper
    return decorator

def hello(name):
    return 'Hello {}!'.format(name)

hello('World')  # returns '<b><i>Hello World!</i></b>'

Now we have two levels of scoping; tag comes from the local namespace of format(), while func is a argument for the decorator() function; both are used by wrapper().

share|improve this answer
The fancy name for these are "closures" – jozefg Jul 22 '13 at 19:03
What is this @make_bold? I have just reached the classes part in tutorials and haven't seem them till now. – Aseem Bansal Jul 23 '13 at 8:57
@AseemBansal: It is a decorator. Everything after the @ must result in a function, and that function is passed the function below the line, and the return value of the decorator replaces that function. Effectively, @make_bold added before def hello(...) results in hello = make_bold(hello) right after hello has been evaluated. – Martijn Pieters Jul 23 '13 at 9:01
Instead of It lets you pass functions around that it might be better to use It lets you pass around those functions which. It's confusing because of use around that in the sentence as in Can you go around that corner?. Still reading your answer and trying to understand closures. I'll reply after spending some time on the wikipedia page. – Aseem Bansal Jul 23 '13 at 9:14
@AseemBansal: I can update that, but 'to pass around' has that meaning, quite separate from 'around the corner'. – Martijn Pieters Jul 23 '13 at 9:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.