Python critique

Stefan Behnel stefan_ml at behnel.de
Fri Dec 10 18:25:47 EST 2010


Benjamin Kaplan, 11.12.2010 00:13:
> On Fri, Dec 10, 2010 at 5:46 PM, Octavian Rasnita wrote:
>> 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.

Why would it?


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




More information about the Python-list mailing list