K
Python does have scope. The problem is not the lack of scope, toOctavian said:It is true that Python doesn't use scope limitations for variables?
Octavian
the shadow declaration of some python construct in the current scope.
print x # raise NameError
[x for x in range(10)] # shadow declaration of x
print x # will print 9
From: "John Nagle said:I have criticisms of Python, but those aren't them.
Probably the biggest practical problem with CPython is
that C modules have to be closely matched to the version of
CPython. There's no well-defined API that doesn't change.
This make life easier for Guido and tough on everybody else.
That's the real cause of Python's "version hell".
On the scope front, given the lack of declarations, Python
has done reasonably well. Scopes in Python aren't as narrow
as one might like, but this isn't a major headache.
Probably the biggest practical problem with CPython is
that C modules have to be closely matched to the version of
CPython. There's no well-defined API that doesn't change.
How narrow are the scopes in Python?
Is each block (each level of indentation) a scope?
If it is, then I think it is very enough because the other cases can be detected easier or it might not appear at all in a well-written program.
If it is not, then yes, it is a problem.
Can you please tell me how to write the following program in Python?
my $n = 1;
{
my $n = 2;
print "$n\n";
}
print "$n\n";
If this program if ran in Perl, it prints:
2
1
I have tried to write it, but I don't know how I can create that block because it tells that there is an unexpected indent.
Thanks.
Octavian
How narrow are the scopes in Python?
Is each block (each level of indentation) a scope?
If it is, then I
think it is very enough because the other cases can be detected easier
or it might not appear at all in a well-written program.
If it is not, then yes, it is a problem.
Can you please tell me how to write the following program in Python?
my $n = 1;
{
my $n = 2;
print "$n\n";
}
print "$n\n";
If this program if ran in Perl, it prints:
2
1
I have tried to write it, but I don't know how I can create that block
because it tells that there is an unexpected indent.
Probably the biggest practical problem with CPython is
that C modules have to be closely matched to the version of
CPython. There's no well-defined API that doesn't change.
This make life easier for Guido and tough on everybody else.
That's the real cause of Python's "version hell".
The only scopes Python has are module and function. Python doesn't
have declarations like Perl (or most other languages), so there's no
way to specify that you want a different variable within the block.
Benjamin Kaplan, 11.12.2010 00:13:
There's more. Both a lambda, and in Python 3.x,
list comprehensions, introduce a new scope.
John Nagle
There's more. Both a lambda, and in Python 3.x,
list comprehensions, introduce a new scope.
From: "Steven D'Aprano said:Can you please tell me how to write the following program in Python?
my $n = 1;
{
my $n = 2;
print "$n\n";
}
print "$n\n";
If this program if ran in Perl, it prints:
2
1
Lots of ways. Here's one:
n = 1
class Scope:
n = 2
print n
print n
Here's another:
n = 1
print (lambda n=2: n)()
print n
Here's a third:
n = 1
def scope():
n = 2
print n
scope()
print n
Here's a fourth:
import sys
n = 1
(sys.stdout.write("%d\n" % n) for n in (2,)).next()
print n
In Python 3, this can be written more simply:
n = 1
[print(n) for n in (2,)]
print n
I have tried to write it, but I don't know how I can create that block
because it tells that there is an unexpected indent.
Functions, closures, classes and modules are scopes in Python. If you
want a new scope, create one of those.
And classes and methods.
Also, class scope and instance scope, though similar, are distinct
scopes. Python also have the hidden interpreter-level scope (the
__builtins__).
Also, class scope and instance scope, though similar, are distinct
scopes. Python also have the hidden interpreter-level scope (the
__builtins__).
Local scopes like described above by Steven are not constructs that areOctavian said:Lots of ways. Here's one:Can you please tell me how to write the following program in Python?
my $n = 1;
{
my $n = 2;
print "$n\n";
}
print "$n\n";
If this program if ran in Perl, it prints:
2
1
n = 1
class Scope:
n = 2
print n
print n
Here's another:
n = 1
print (lambda n=2: n)()
print n
Here's a third:
n = 1
def scope():
n = 2
print n
scope()
print n
Here's a fourth:
import sys
n = 1
(sys.stdout.write("%d\n" % n) for n in (2,)).next()
print n
In Python 3, this can be written more simply:
n = 1
[print(n) for n in (2,)]
print n
Functions, closures, classes and modules are scopes in Python. If youI have tried to write it, but I don't know how I can create that block
because it tells that there is an unexpected indent.
want a new scope, create one of those.
Hi Steven,
Thank you for your message. It is very helpful for me.
I don't fully understand the syntax of all these variants yet, but I can see that there are more scopes in Python than I thought, and this is very good.
Octavian
n = 1
[print(n) for n in (2,)]
print n
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.