Python strict mode?

P

Peng Yu

Hi,

Is there is a way to make python check the variables just as the
strict mode in perl. Would somebody let me know what is the python
equivalent to the perl strict mode?

Regards,
Peng
 
A

Andreas Waldenburger

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.

/W
 
P

Patrick Sabin

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.

This can be easier accomplished using __slots__, e.g.:
.... __slots__ = ['a']
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.

I agree.

- Patrick
 
T

Terry Reedy

Peng said:
Hi,

Is there is a way to make python check the variables just as the
strict mode in perl. Would somebody let me know what is the python
equivalent to the perl strict mode?

3rd party code checkers like pylint, pychecker.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top