Python equivalent of 'use strict'

Hamish Lawson hamish_lawson at yahoo.co.uk
Tue Jan 16 13:15:01 EST 2001


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/



More information about the Python-list mailing list