Subroutine Equivalent in Python (newbie Q)

Carel Fellinger cfelling at iae.nl
Thu Feb 1 23:44:26 EST 2001


binnc at my-deja.com wrote:
> Hello,

> Another question from someone totally new to
> Python. Is there an equivalent in Python to a sub-
> routine, (e.g. gosub and return).  I want to
> create a modular program with sub-routines to
> perform distinct tasks wihin the program for
> organizational and debugging purposes, etc.  Is
> the only (or best) way to do this is with
> modules?  A function works but the values
> obtained within the function do not appear to be
> valid outside of that function.  I guess I am

Ai, I fear your BASIC-abuse is shining through here:) IIRQ from the
one week I had to work with BASIC, it offered the most basic form of
subroutine calls possible, no context saving, no new var scope, only
a return to were you came from.  So basicly there was but one scope in
such a program, comparable to the global scope of Python.  In almost
any other programming language I know of a subroutine creates a new
namespace/scope.  The joy of this is that it prevents nasty, hard to
find nameclashes; ie in sub A you choose i for a local var, you go off
to sub B, sub C etc, and somewhere in that line som other sub also
choose to use i as a local var, but helas it isn't a local var, so
the program logic of sub A might break in unpredictable ways.

> looking for the best approach to create the
> subroutines for execution from the main flow of
> the program.

There are several tools available to lumb code together to perform
reoccuring tasks.  The most basic one is the function, like:

>>> def fun(args):
>>>     return result

In its pure form it performs some calculation on its args and returns
a result.  But Python is not a pure functional language:) so other
things are possible here to, like:

>>> a = "spam"
>>> def more_fun(args):
>>>     print a, args     # a is automatically looked up in the outer scope
... 
>>> more_fun("and spam")
spam and spam

And for yet some more trickery:

>>> a = "spam, spam"
>>> def more_fun(args):
>>>     global a         # this is needed to be able to assign to the global a
>>>     a = a + ', ' + args
... 
>>> more_fun("spam and spam")
>>> print a
spam, spam, spam and spam
>>> more_fun("spam and spam")
>>> print a
spam, spam, spam and spam, spam and spam

The trick is here that the global keyword tells Python to look a up in
the global scope and not in the local function scope.  But global scope
get's hairy quickly, and func within func still only know of two scopes,
their own and the module-global scope.  There are better ways of doing
things, like:

>>> class C:
>>>     a = 'spam'
>>>     def __init__(self, s='more spam'):
>>>         self.s = self.a + 'and ' + s
>>>     def prt(self):
>>>         print self.s
...
>>> c = C()
>>> cc = C('spam and eggs')
>>> c.prt()
spam and more spam
>>> cc.prt()
spam and spam and eggs
>>> cc.s = 'no more spam'
>>> cc.prt()
no more spam
>>> c.prt()
spam and more spam


So a class is ideally suited to lumb together some code (several funcs
in a single class) and some data to go along with that code.  Read all
about it in the FIne Manuals
-- 
groetjes, carel



More information about the Python-list mailing list