Suggestion: Python global scope

A

Anonymous Bastard

I've been tossing this idea in my mind for some time now:

In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.

But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope. For instance:

Code:
global X
X = 1

def P():
     X = 2
     print X
     global X
     print X

print X
P()
print X
[code]

Currently, this will print 1, 2, 2 and 2. But if global would be limited 
to current scope, it would print 1, 2, 1, 1.

'X = 2' would work on the local version of X, 'global X' will 'import' 
the global X into the local scope, so any actions on X would reference 
the global X, rather than previous X.
 
G

Gerhard Häring

Anonymous said:
I've been tossing this idea in my mind for some time now:

In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.

But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope. For instance:

Code:
global X
X = 1

def P():
X = 2
print X
global X
print X

print X
P()
print X
[code]

Currently, this will print 1, 2, 2 and 2. But if global would be limited 
to current scope, it would print 1, 2, 1, 1.

'X = 2' would work on the local version of X, 'global X' will 'import' 
the global X into the local scope, so any actions on X would reference 
the global X, rather than previous X.[/QUOTE]

Anything backwards incompatible like this will never happen.

-- Gerhard
 
J

John Roth

I've been tossing this idea in my mind for some time now:

In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.

But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope. For instance:

Code:
global X
X = 1

def P():
     X = 2
     print X
     global X
     print X

print X
P()
print X
[code]

Currently, this will print 1, 2, 2 and 2. But if global would be limited
to current scope, it would print 1, 2, 1, 1.

'X = 2' would work on the local version of X, 'global X' will 'import'
the global X into the local scope, so any actions on X would reference
the global X, rather than previous X.[/QUOTE]

I suppose it could be done, but I for one would be against it on
several grounds.

First, global operates somewhat as a declaration currently.
Declarations are easier for most developers to understand than
redefinitions of a variable on the fly.

Second, following up the first point, it redefines a variable. While
this is common practice, it's unfortunate since it leads to some
amount of confusion. It also seems like it would be most useful in
large, poorly structured methods, which is exactly the wrong way to
go.

Finally, as Gerhard Haring has said, it's backward incompatible, so
the window for considering it has past: the 3.x series is pretty much
frozen.

John Roth
 
A

André

The function of the global keyword is to 'push' local variables to the
global scope. If you want to 'import' a variable into a local scope,
pass the variable to your function/method as an argument.
Anonymous said:
I've been tossing this idea in my mind for some time now:
In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.
But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope. For instance:
Code:
global X
X = 1[/QUOTE]
[QUOTE]
def P():
    X = 2
    print X
    global X
    print X[/QUOTE]
[QUOTE]
print X
P()
print X
[code][/QUOTE]
[QUOTE]
Currently, this will print 1, 2, 2 and 2. But if global would be limited
to current scope, it would print 1, 2, 1, 1.[/QUOTE]
[QUOTE]
'X = 2' would work on the local version of X, 'global X' will 'import'
the global X into the local scope, so any actions on X would reference
the global X, rather than previous X.[/QUOTE]
[/QUOTE]
Alternatively, inside the function, you can have
_X = X
and use _X afterwards.  This is a lot more explicit imo.  Giving a
different meaning to "global" like the one you suggest would make it
extremely difficult to mimic its current behaviour - whereas what you
want to do can be done with a simple change assignment (or passing an
argument to the function).

Ask yourself: given change Y that I propose to Python, how would I
still be able to do Z which is the current behaviour and is found to
be useful by others?

André
 
C

Carl Banks

I've been tossing this idea in my mind for some time now:

In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.

But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope.

No way. You'd need to find another keyword. "localize" might be a
better word to use.

For instance:

Code:
global X
X = 1

def P():
     X = 2
     print X
     global X
     print X

print X
P()
print X
[code]

Currently, this will print 1, 2, 2 and 2. But if global would be limited
to current scope, it would print 1, 2, 1, 1.[/QUOTE]

What would it print if you changed P to this?

def P():
     X = 2
     print X
     global X
     print X
     X = 3

Would it print 1, 2, 1, 1 or 1, 2, 1, 3?

[QUOTE]
'X = 2' would work on the local version of X, 'global X' will 'import'
the global X into the local scope, so any actions on X would reference
the global X, rather than previous X.[/QUOTE]

Do you have a specific use case in mind?  Where would this sort of
thing be useful?

(The be sure, I can think of a use case for something like this,
namely optimizing away hash lookups for frequently used globals.  But
such an optimization doesn't justify new syntax, especially when you
can already do it only slightly less conveniently.)


Carl Banks
 
A

alex23

Anonymous said:
In Python, declaring a variable using the global statement automatically
makes it available in all subsequent scopes.

But to me, it makes more sense to use the global statement to 'import' a
variable from the global scope into the current scope.

My understanding is that this is exactly what the 'nonlocal' keyword
in Python 3.0 provides.

http://www.python.org/dev/peps/pep-3104/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top