Does python have a “use strict;” and “use warnings;” like in perl?

To provide an answer that perhaps avoids a little of the commentary noise here, I’ll try another one.

The two pragmata in your original question really expand to:

use strict "vars";
use strict "refs";
use strict "subs";
use warnings;

To answer each in turn:

  • The effect of use strict "vars" is to cause a compile-time error to refer to a variable without first declaring that it exists (such as is the default in more static languages such as C, C++ and Java). Because Python does not have specific syntax to declare that a variable exists, it has no equivalent. Assigning to a name in Python always creates it if it didn’t exist first. This feature of strict has no Python equivalent and the safety it provides cannot be recreated.

Eg:

$ perl -c -e 'use strict "vars"; $foo = 1'
Global symbol "$foo" requires explicit package name at -e line 1.
-e had compilation errors.

$ perl -c -e 'no strict "vars"; $foo = 1'
-e syntax OK
  • The effect of use strict "refs" is to disallow the use of plain strings containing the name of an (existing or new) variable as a reference to the variable itself. Python does not do this so has no need to disable it.

Eg:

$ perl -e 'use strict "refs"; ${"message"} = "hello"; print $message'
Can't use string ("message") as a SCALAR ref while "strict refs" in use at -e line 1.

$ perl -e 'no strict "refs"; ${"message"} = "hello"; print $message'
hello
  • The effect of use strict "subs" is to cause a compile-time any attempt to call a function that is known not to exist. Python does not perform any such checking, and has no way to enable such a feature.

Eg:

$ perl -c -e 'use strict "subs"; foo'
Bareword "foo" not allowed while "strict subs" in use at -e line 1.
-e had compilation errors.

$ perl -c -e 'no strict "subs"; foo'
-e syntax OK
  • The effect of use warnings is to enable more warnings at both compile- and runtime of various categories of behaviour that was default in earlier versions, may at times be desired, or which has never been a good idea but isn’t strictly an error. For example, the use of uninitialised values as numbers ought usually to give a warning, but originally it did not do so.

Eg:

$ perl -e 'use warnings; my $u; print 2 + $u'
Use of uninitialized value $u in addition (+) at -e line 1.
2

$ perl -e 'no warnings; my $u; print 2 + $u'
2

Finally; some comments have been made that Python has similar functionality in __future__. However, this should not be considered similar to Perl’s pragmata, as most of the latter are lexically-scoped, and can be enabled or disabled within small scopes as required; where’s Python’s __future__ is only enabled for an entire source file.

Eg.

use strict;
use warnings;

my $total;

$total += count_things($_) for @list;

{
   no warnings 'uninitialized';
   printf "The total is %d\n", $total;
}

A somewhat-contrived example, but this one demonstrates the use of no warnings 'uninitialized' to disable the warning about using an uninitialised value simply within the printf statement, while still keeping the other warnings enabled everywhere else.


In summary then: Python does not have a use strict or any near-equivalent as any of the safety features it provides are either mandatory or not available in the Python language, and does not have a use warnings. Those features it does provide are enabled only at the file-level and cannot be selectively enabled or disabled per scope.


Edit: Actually I have now been informed that Python does have some controllable warning flags, that can be enabled and disabled as required.

Leave a Comment