Python equivalent of 'use strict'
Oldayz
sill at localhost.kitenet.net
Tue Jan 16 21:58:17 EST 2001
On Tue, 16 Jan 2001 18:15:01 GMT, Hamish Lawson <hamish_lawson at yahoo.co.uk> wrote:
>David Lees wrote:
>
>> Is there an equivalent to the perl 'use strict'? [My perl/C++ guru
>> friend] is concerned with accidentally initializing values of
>> mistyped variables. I showed him how you get an error doing an
>> operation with an uninitialized variable ... but he is looking for
>> this 'use strict' stuff.
>
>Python effectively has "use strict" on all the time and all assignments
>effectively use "my". This in Python
>
> x = 3
> z = 2
> x = z + 5
> z = y
>
>is equivalent to this in Perl:
>
> use strict;
> my $x = 3;
> my $z = 2;
> my $x = $z + 5;
> my $z = $y;
>
>Both will complain about the uninitialised y.
>
>But "use strict" couldn't stop you from saying
>
> my $foo = 2;
> my $fooo = $foo + 1;
>
>where you meant to say
>
> my $foo = 2;
> my $foo = $foo + 1;
>
>(though I concede that using 'my' on both assignments is not quite
>idiomatic Perl). Similarly, Python won't stop you saying:
>
> foo = 2
> fooo = foo + 1
>
>where you meant
>
> foo = 2
> foo = foo + 1
>
>Hamish Lawson
>
>
>Sent via Deja.com
>http://www.deja.com/
Would it perhaps be useful to have to indicate specifically that you're
declaring a new variable with a keyword?
import strict
new a
new b = 5
a = b + 6
c = 3 # raises an error
I'm not sure I'd like it - I only used perl in a few tiny scripts and
never did use strict, so I don't know how it'd feel.
--
Andrei
More information about the Python-list
mailing list