[Tutor] nested functions

Evert Rol evert.rol at gmail.com
Sat Sep 4 13:25:51 CEST 2010


> hello,
> i have to plug two functions b() and c() inside another one a();
> i wonder if the code defining b and c must be in the same text file of
> a or it is possible to import b and c somehow, hence giving the code a
> neater appearance

Definitely!
Read through http://docs.python.org/tutorial/modules.html, that should definitely get you started.

Basically, with b & c defined in file1.py (choose your own appropriate name), from file2.py you can do:

import file1

def a(...):
    file1.b()
    file1.c()

or (bit shorter, but can be less clear):

from file1 import b, c

def a(...):
    b()
    c()



  Evert



More information about the Tutor mailing list