problem using using list with function
kseehof at neuralintegrator.com
Thu Oct 11 01:08:41 CEST 2001
Things have to be defined before they can be used. For example:
print x # raises NameError exception
x = 5
Of course, this won't work since x is used before it is defined.
In python, function definitions are executed at runtime, so a
def statement is analogous to an assignment such as x=5.
If you are used to compiled languages you might have expected
different behaviour since compilers usually process function
definitions before runtime.
In python, functions are objects just like everything else.
Now this won't be surprising:
f() # raises NameError exception
adam griffin wrote:
> I think I figured out my problem. I had the function definition at the end
> of my script when I put it at the beginning it worked. is this the only
> place you can put definitions?
> "Ken Seehof" <kseehof at neuralintegrator.com> wrote in message
> news:mailman.1002701769.4408.python-list at python.org...
> > > i'm trying to write a function that takes a list as it's only
> > and
> > > also has a list as it's return value. this is sort of what it looks
> > >
> > > list1 = [ 1, 2, 3];
> > >
> > > list2 = function(list1)
> > >
> > > def function(listArguement):
> > > #statements...
> > > return list
> > Looks like you are on the right track. Try this in your python
> > You can learn a lot by trying things in the interpreter to see what
> > >>> def spam_and(a):
> > ... b = ['spam', 'spam'] + a
> > ... return b
> > ...
> > >>> list1 = [ 1, 2, 3]
> > >>> list2 = spam_and(list1)
> > >>> print list2
> > ['spam', 'spam', 1, 2, 3]
More information about the Python-list