Python strict mode?

Discussion in 'Python' started by Peng Yu, Sep 13, 2009.

  1. Peng Yu

    Peng Yu Guest

    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
    Peng Yu, Sep 13, 2009
    #1
    1. Advertising

  2. 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!
    Andreas Waldenburger, Sep 13, 2009
    #2
    1. Advertising

  3. > 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
    Patrick Sabin, Sep 13, 2009
    #3
  4. Peng Yu

    Terry Reedy Guest

    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.
    Terry Reedy, Sep 13, 2009
    #4
  5. Peng Yu

    Carl Banks Guest

    Carl Banks, Sep 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    470
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    457
    Edward Loper
    Aug 7, 2007
  3. John J Lee
    Replies:
    0
    Views:
    511
    John J Lee
    Aug 7, 2007
  4. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    425
    Edward Loper
    Aug 9, 2007
  5. Neal Becker
    Replies:
    0
    Views:
    376
    Neal Becker
    Aug 9, 2007
Loading...

Share This Page