FAQ
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

Search Discussions

  • Andreas Waldenburger at Sep 13, 2009 at 7:22 pm

    On Sun, 13 Sep 2009 09:25:50 -0700 (PDT) Peng Yu wrote:

    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

    --
    INVALID? DE!
  • Patrick Sabin at Sep 13, 2009 at 8:04 pm

    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.:
    class X(object):
    ... __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
  • Terry Reedy at Sep 13, 2009 at 9:21 pm

    Peng Yu wrote:
    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.
  • Ben Finney at Sep 13, 2009 at 9:23 pm

    Peng Yu <pengyu.ut at gmail.com> writes:

    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?
    There isn't one, because Python doesn't have the same concept of ?unsafe
    constructs?; in Python, we think more carefully about adding such
    constructs to the language in the first place :-) and use a process like
    Python 2 ? Python 3 to remove them.

    You're specifically asking about variables, so I assume you mean the
    ?strict vars? mode to chack variable usage against variable
    declarations. Python doesn't have those declarations, and doesn't really
    have ?variables? as you might think of them either. So no, there's no
    such thing in Python.

    You will probably be interested in static code checkers such as pyflakes
    <URL:http://pypi.python.org/pypi/pyflakes> which is small and simple, or
    pylint <URL:http://pypi.python.org/pypi/pylint> which is comprehensive
    and very customisable.

    --
    \ ?It ain't so much the things we don't know that get us in |
    `\ trouble. It's the things we know that ain't so.? ?Artemus Ward |
    _o__) (1834?1867), U.S. journalist |
    Ben Finney
  • Carl Banks at Sep 13, 2009 at 11:42 pm

    On Sep 13, 9:25?am, Peng Yu wrote:
    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?

    I don't recommend using it, but see this post:

    http://groups.google.com/group/comp.lang.python/msg/290e4617c3a97f6f?hl=en


    Carl Banks

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 13, '09 at 4:25p
activeSep 13, '09 at 11:42p
posts6
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase