473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Python scope is too complicated

Max
Yeah, I know. It's the price we pay for forsaking variable declarations.
But for java programmers like me, Py's scoping is too complicated.
Please explain what constitutes a block/namespace, and how to refer to
variables outside of it.
Jul 18 '05 #1
7 2027
On Sun, 20 Mar 2005 13:53:34 +0200, rumours say that Max
<rabkin@mweb[DOT]co[DOT]za> might have written:
Yeah, I know. It's the price we pay for forsaking variable declarations.
But for java programmers like me, Py's scoping is too complicated.
Please explain what constitutes a block/namespace, and how to refer to
variables outside of it.


I presume you read "4.1 Naming and binding" from Python reference manual.

"What constitutes a block" is answered in paragraph 2, for example. Please come
back with confusing / missing information in that chapter giving us a chance to
improve the documentation.

Cheers!
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #2
jfj
Max wrote:
Yeah, I know. It's the price we pay for forsaking variable declarations.
But for java programmers like me, Py's scoping is too complicated.
Please explain what constitutes a block/namespace, and how to refer to
variables outside of it.

Some may disagree, but for me the easiest way to understand python's
scopes is this:

"""In Python, there are only two scopes. The global and the local.
The global scope is a dictionary while the local, in the case of a
function is extremely fast. There are no other scopes. There are
no scopes in the nested statements inside code blocks and there are
no class scopes. As a special case, nested function definitions
appear to be something like a nested scope, but in reallity this is
detected at compile-time and a strange feature called 'cell variables'
is used"""

In order to write to a global variable from a function, we have
to use:
global var

which notifies the compiler that assignment to 'var' does not make
a new local variable, but it modifies the global one (OT: wouldn't
"global.var = 3" be nicer?). On the other hand, if we just want to
read a global variable we don't have to say "global var" because
the compiler sees that there is no assignment to 'var' in the function
code and therefore intelligently concludes that it's about a global
one.

Generally, python sees everything as
exec "code" in global_dictionary, local_dictionary

In this case it uses the opcode LOAD_NAME which looks first in locals
and then in globals (and actually, then in __builtins__)

For functions it uses either LOAD_FAST for locals or LOAD_GLOBAL for
globals.

HTH

jfj
Jul 18 '05 #3
jfj wrote:
Max wrote:
Yeah, I know. It's the price we pay for forsaking variable declarations.
But for java programmers like me, Py's scoping is too complicated.
Please explain what constitutes a block/namespace, and how to refer to variables outside of it.

Some may disagree, but for me the easiest way to understand python's
scopes is this:

"""In Python, there are only two scopes. The global and the local.
The global scope is a dictionary while the local, in the case of a
function is extremely fast. There are no other scopes.


This isn't true anymore, now that generator comprehensions have been
added to the language.
x = 17
sum(x for x in xrange(101)) 5050 x

17

Jul 18 '05 #4
Hi All--

Dan Bishop wrote:
"""In Python, there are only two scopes. The global and the local.
The global scope is a dictionary while the local, in the case of a
function is extremely fast. There are no other scopes.


This isn't true anymore, now that generator comprehensions have been
added to the language.
x = 17
sum(x for x in xrange(101)) 5050 x

17


The equivalent in list comprehensions which currently allows the x to
leak out into its containing scope is going away soon. Will that be
another scope? Or are generator and list comprehensions only one scope?

Metta,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 18 '05 #5
jfj
Dan Bishop wrote:
x = 17
sum(x for x in xrange(101))
5050
x


17


Your example with generator expressions is interesting.
Even more interesting is:

def foo(x):
y= (i for i in x)
return y

From the disassembly it seems that the generator is a code object but
'x' is not a cell variable. WTF? How do I disassemble the generator?
Must look into this....

[ list comprehensions are different because they expand to inlined
bytecode ]

jf

Jul 18 '05 #6

"jfj" <jf*@freemail.gr> wrote in message news:42************@freemail.gr...
def foo(x):
y= (i for i in x)
return y

From the disassembly it seems that the generator is a code object
What is type(foo([1,2,3])) ?
but 'x' is not a cell variable. WTF?


As I understand it, the object 'x' binds to is immediately used to create
the generator object. The local name is just a dummy that is not part of
the result. I believe that the above is equivalent to

def foo(x):
def _(z):
for i in z: yield i
return _(x)

although I suspect that the implementation builds the generator more
directly.

Terry J. Reedy

Jul 18 '05 #7
jfj wrote:
def foo(x):
y= (i for i in x)
return y

From the disassembly it seems that the generator is a code object but
'x' is not a cell variable. WTF?
That's because x is not assigned to anywhere in the
body of foo. The bytecode compiler optimizes away the
creation of a cell in this case, just passing the
value of x as an implicit parameter to the generator.
How do I disassemble the generator?


You'd have to get hold of the code object for it
and disassemble that. There should be a reference to
it in one of the co_consts slots, I think.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
31
by: metaperl | last post by:
-- python -i File "<stdin>", line 1 class = "algebra" ^ SyntaxError: invalid syntax Why isn' t the parser smart enough to see that class followed by an identifier is used for class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.