Defining and calling functions in Python

Darrell news at dorb.com
Mon Jan 1 10:32:44 EST 2001


Code is executed in the order seen.
So you can't call a function before it's defined.

Try this:

def a():
    print "-a1-"

a()

def a():
    print "-a2-"

a()

Or for increased fun with the letter 'a':
>>> def a():
...     print "-a1-"
...
>>> def a(a=a):
...     a()
...     print "-a2-"
...
>>> a()
-a1-
-a2-
>>>

--Darrell

"Stein Surland" wrote:
> I have this function:
>
> def WriteToFile(Outfile, Filecontent):
>     Utopened = []
>     Utopened = open(Outfile, 'w')
>     Utopened.write(string.join(Filecontent,'\n'))
>     Utopened.close()
>
> When this function is called with:
>
>      WriteToFile('file.txt', 'Some text')
>
> before the function is declared, I get this error message:
>
> Traceback (innermost last):
>   File "./RPMParser.py", line 21, in ?
>     WriteToFile('file.txt', 'Some text')
> NameError: WriteToFile
>
>
> But when I move the functioncall after the definition, it works OK. What
> am I missing
> here? I thought, as Python is an interpreted language, that I didn't
> need to "declare in order", as in C++?
>
> Stein
> --
>
> Stein Surland   Trying is the first step towards failure
> ssurland at online.no - Homer Simpson
> http://home.online.no/~ssurland/
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list