Python strict mode?

Andreas Waldenburger usenot at geekmail.INVALID
Sun Sep 13 21:22:36 CEST 2009

On Sun, 13 Sep 2009 09:25:50 -0700 (PDT) Peng Yu <pengyu.ut at>

> Is there is a way to make python check the variables just as the
> strict mode in perl.
Short answer: No.

Long answer: I'm guessing you want Python to complain when assigning to
a variable that has not been declared before. Since Python has no
declarations, you're plain out of luck.

> Would somebody let me know what is the python equivalent to the
> perl strict mode?
I don't know about any equivalents (but I wouldn't be surprised if
there were libraries for that somewhere).

You could write a class with a custom __setattr__() method that checks
for valid attribute names for that class (a list of strings given to
it's __init__() method). That way you could form several restricted
"namespaces" for variables simply as different instances of that class.

But in my opinion, it isn't worth it. You still don't get compile time
errors, which is probably the main reason to use strict mode.

Can you describe why you (think you) need this? Maybe there is a
more pythonic approach.



More information about the Python-list mailing list