Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > amazing scope?

Reply
Thread Tools

amazing scope?

 
 
andrea crotti
Guest
Posts: n/a
 
      11-30-2012
I wrote a script, refactored it and then introducing a bug as below:

def record_things():
out.write("Hello world")

if __name__ == '__main__':
with open('output', 'w') as out:
record_things()


but the shocking thing is that it didn't actually stopped working, it
still works perfectly!

What my explanation might be is that the "out" is declared at module
level somehow,
but that's not really intuitive and looks wrong, and works both on
Python 2.7 and 3.2..
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      11-30-2012
Am 30.11.2012 12:11, schrieb andrea crotti:
> I wrote a script, refactored it and then introducing a bug as below:
>
> def record_things():
> out.write("Hello world")


This is a function. Since "out" is not a local variable, it is looked up
in the surrounding namespace at the time the function is called.


> if __name__ == '__main__':
> with open('output', 'w') as out:
> record_things()


This is not in a function, so binding variables affects the containing
namespace. This includes binding the context manager to "out". Note that
you could have moved the whole code into a function (e.g. one called
main()) and then you would have gotten the expected failure.


> What my explanation might be is that the "out" is declared at module
> level somehow, but that's not really intuitive and looks wrong, and
> works both on Python 2.7 and 3.2..


Other than in C/C++/Java and others, indention doesn't introduce a new
scope, but I understand your intuition. Even simpler, this is how my
early Python code looked like, in the absence of the C ternary operator
and the distinction between a variable declaration and an assignment:

foo = None
if some_condition:
foo = 'bar'
else:
foo = 'baz'

More idiomatic would have been this:

if some_condition:
foo = 'bar'
else:
foo = 'baz'


Summary: I'd say that everything is fine.

Uli

 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      11-30-2012
On Sat, Dec 1, 2012 at 3:05 AM, andrea crotti <(E-Mail Removed)> wrote:
> Well I knew that this works fine, even if I feel a bit guilty to do
> this, and better is:
>
> foo = 'bar' if some_condition else 'baz'
>
> Anyway for me the suprise is that something that is defined *later* at
> the module scope is found in a function which is defined *earlier*.


It's assigned to earlier. It's no different from C code like this:

int foo;

void blah()
{
/* do stuff with foo */
}

int main()
{
foo = 1;
}


In Python, the "int foo;" is implicit, but the scope is the same.

ChrisA
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      11-30-2012
On 11/30/2012 11:05 AM, andrea crotti wrote:
> Well I knew that this works fine, even if I feel a bit guilty to do
> this, and better is:
>
> foo = 'bar' if some_condition else 'baz'
>
> Anyway for me the suprise is that something that is defined *later* at
> the module scope is found in a function which is defined *earlier*.


It would have been nice if you had indicated in your original post just
*what* you considered "shocking" about the code sample. The code was
valid, and we all had to guess what about it was puzzling you. Like
most others, I first guessed you were confused about the definition
being in an if statement. Then I thought you were confused by the with
statement, since it's not as obvious that "as" binds the name to the
object in the same way as assignment.

But I never would have guessed that you were confused about the order of
execution in a module.

The source code in a module is executed in order, including function
definitions defined at top level. However, such a function definition's
execution creates a function object, and assigns a global name (usually)
to that object. It does not "execute" the object. When that function
is executed later, it then looks for global variables for stuff that's
not defined within its own scope. So by the time the function
references the 'out' name, it's been successfully added to the global
namespace.



--

DaveA

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Amazing hardware, Amazing software, Amazing Stories Mike Painter HTML 0 11-02-2006 11:18 PM
Best Studying Matrial + Amazing Cost + 3 in price of 1 JohnCena MCSD 1 02-21-2006 10:42 AM
Best Studying Matrial + Amazing Cost + 3 in price of 1 JohnCena MCSD 0 01-11-2006 12:49 PM
Re: Bulk Newsgroup Poster - amazing result Malke Wireless Networking 0 01-17-2005 01:35 AM
This is simply amazing...... Julius MCSE 1 09-11-2003 04:54 PM



Advertisments