Python critique

O

Octavian Rasnita

It is true that Python doesn't use scope limitations for variables?

Octavian

----- Original Message -----
From: "kolo 32" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Friday, December 10, 2010 12:31 PM
Subject: Python critique
 
J

Jean-Michel Pichavant

Octavian said:
It is true that Python doesn't use scope limitations for variables?

Octavian
Python does have scope. The problem is not the lack of scope, to
problem is 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

it can become a problem if you write such code:

index = 1
myNewList = [index*2 for index in [1,2,3,4,5]]
print myNewList(index) # here most new commer would want to use index=1.

To compare to some C construct, python declares the variable in the
current scope while the C construct will create a new scope for the
following block.

IMO, i don't see this one as an issue, because even when coding in C,
you do NOT use the same name for 2 different things. Python shadow
declarations would be a problem for someone used to take "advantage" of
the C construct.
The author states that this is bug prone, I did write a lot of python
lines, never happened to me.

JM

PS : pylint reports such shadow declaration, W0631: 4: Using possibly
undefined loop variable 'x'
 
S

Stefan Behnel

Jean-Michel Pichavant, 10.12.2010 15:02:
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

Note that this is rarely a problem in practice, and that this has been
fixed in Python 3.

Stefan
 
J

John Nagle

Hi, all,

Python critique from strchr.com:

http://www.strchr.com/python_critique

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.
Python has been more successful with the "no declarations"
rule than most other languages. Others that started with no
declarations, from FORTRAN to JavaScript, eventually had to retrofit
them.

Interestingly, some of the motivation for
"functional programming" in Python, with lambdas and list
comprehensions, is to get a very local scope for a very local
variable.

John Nagle
 
O

Octavian Rasnita

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.

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
 
B

Benjamin Kaplan

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


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.
 
S

Steven D'Aprano

How narrow are the scopes in Python?
Is each block (each level of indentation) a scope?

Thankfully, no.

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.

I don't understand what this means.

If it is not, then yes, it is a problem.

Why is it 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

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.
 
S

Stefan Behnel

John Nagle, 10.12.2010 21:02:
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.

Well, there are no huge differences between CPython versions (apart from
the Py_ssize_t change back in 2.5), and porting isn't as hard as you seem
to suggest here.

This make life easier for Guido and tough on everybody else.
That's the real cause of Python's "version hell".

If you write your code in Cython instead of C, it's a lot easier to keep it
portable across CPython versions (and C compilers). It's also a lot easier
to write it in the first place.

Note also that Py3.2 introduces a stable ABI for extension modules. This
implies a stable API as well.

Stefan
 
S

Stefan Behnel

Benjamin Kaplan, 11.12.2010 00:13:
Why would it?

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.

Yep, keeps you from writing bad style code like the above.

Stefan
 
D

Dan Stromberg

  There's more.  Both a lambda, and in Python 3.x,
list comprehensions, introduce a new scope.

                       John Nagle

And classes and methods.

Also, I like to draw a distinction between "code scopes" and "data
scopes". We've been talking about data scopes. Code scopes are
pretty much indents and dedents in Python - they define what code is
operated on by something code-flow-related.

To the OP: Python doesn't need a "my" or "local" - it has more
rational defaults.
 
S

Stefan Behnel

John Nagle, 11.12.2010 00:51:
There's more. Both a lambda, and in Python 3.x,
list comprehensions, introduce a new scope.

To be a little more precise: in Py3, list comprehensions use the same kind
of scope that generator expressions, set comprehensions and dict
comprehensions always had.

Stefan
 
O

Octavian Rasnita

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.


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
 
L

Lie Ryan

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__).
 
S

Steve Holden

Also, class scope and instance scope, though similar, are distinct
scopes. Python also have the hidden interpreter-level scope (the
__builtins__).

But classes and instances don't have scopes. They have namespaces.

That is, if we are talking about lexical scoping.

regards
Steve
 
S

Steve Holden

Also, class scope and instance scope, though similar, are distinct
scopes. Python also have the hidden interpreter-level scope (the
__builtins__).

Kindly ignore my last post. Class scopes are lexical, instance scopes
are not.

regards
Steve
 
J

Jean-Michel Pichavant

Octavian 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.


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
Local scopes like described above by Steven are not constructs that are
used that often, not to solve any scoping issue (except for the first
example maybe). It's more a demonstration that you can do it with python.
The reason is that Python developpers will not put themself in the
situation where they need to use a variable 'orange' line 32 and use the
same variable 'orange' line 33 to refer to something else.

JM
 

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,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top