Joseph Quigley said unto the world upon 2005-04-13 13:05:

<SNIP>

def silly(this_is_serious):
        print 'this is serious"

But I get an error!
Traceback (most recent call last):
  File "C:\Python24\saved\tmp1.py", line 7, in -toplevel-
    silly(this_is_serious)
NameError: name 'this_is_serious' is not defined

But you can have silly say something silly and also say something serious right? If so.... how?

args has confused me, but I'm slowly picking it up.

Thanks Brian, Jeff, Liam for your help.
        Joseph

Hi Joseph,

you are welcome. :-)

There are at least a couple of things still awry. Take a look:

>>> def silly(this_is_serious):
...     print 'this is serious"   # bad quotes -- no function!
Traceback (  File "<interactive input>", line 2
    print 'this is serious"   # bad quotes -- no function!
                                                         ^
SyntaxError: EOL while scanning single-quoted string
>>> def silly(this_is_serious):
...     print 'this is serious'
...     
>>> silly(42)
this is serious


I think what you are doing is mistaking the role of the name this_is_serious *inside* your function definition and the role of names *outside* your definition. Outside of your definition, you've not made this_is_serious be bound to anything, so it is an undefined name.


>>> some_serious_thing
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'some_serious_thing' is not defined
>>> silly(some_serious_thing)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'some_serious_thing' is not defined

See, it is the same error whether you just try to access the name or send the name as a function argument. The name hasn't been given a value.

And that is still true with your this_is_serious name. You gave it a value, but *only* inside your function definition. Compare:

>>> some_serious_thing = 'My, but you are grave!'
>>> silly(some_serious_thing)
this is serious
>>> this_is_serious = 'Indeed'
>>> silly(this_is_serious)
this is serious
>>>


A few other things: the simple function here can be defined so as to take no arguments at all. After all, it doesn't use them. So:


>>> def simple_silly():
...     print 'this is serious'
...     
>>> simple_silly(42)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: simple_silly() takes no arguments (1 given)
>>> simple_silly()
this is serious
>>>


Also, the names inside and outside your function are independent. Wittiness:


>>> def might_confuse(my_value):
...     print my_value
...     
>>> my_value = 42
>>> your_value = 'something completely different'
>>> might_confuse(your_value)
something completely different
>>>


HTH. Please excuse any incoherency; I'm stuck in bed with my 'Welcome to Spring!' head cold :-)


Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to