Easy questions from a python beginner

bart.c bartc at freeuk.com
Mon Jul 12 04:48:04 EDT 2010


"MRAB" <python at mrabarnett.plus.com> wrote in message
news:mailman.591.1278900548.1673.python-list at python.org...
> Alf P. Steinbach /Usenet wrote:

>>   def foo():
>>       print( blah )
>>       blah = "this is both an assignment and a declaration causing it to
>> exist"
>>
>>   foo()
>>
>> Clearly when the exception is raised, referring to the variable, the
>> variable exists.

> How about this:
>
> >>> def foo():
> print("Before:", locals())
> x = 0
> print("After:", locals())
>
>
> >>> foo()
> Before: {}
> After: {'x': 0}

That's interesting. So in Python, you can't tell what local variables a
function has just by looking at it's code:

def foo(day):
 if day=="Tuesday":
  x=0
 print ("Locals:",locals())

#foo("Monday")

Does foo() have 1 or 2 locals? That might explain some of the difficulties 
of getting Python implementations up to speed.

-- 
Bartc




More information about the Python-list mailing list