Is there a need for a “use strict” Python compiler?

后端 未结 10 2203
眼角桃花
眼角桃花 2021-02-03 21:26

There exist static analysis tools for Python, but compile time checks tend to be diametrically opposed to the run-time binding philosophy that Python embraces. It\'s possibl

10条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 22:21

    Python does have something that can change script syntax:

    from __future__ import print_function
    

    and various other future-features that have syntax implications. It's just that Python's syntax has been stricter, stabler and more well-defined than historical Perl; the kind of things that ‘strict refs’ and ‘strict subs’ prohibit have never existed in Python.

    ‘strict vars’ is primarily intended to stop typoed references and missed-out ‘my’s from creating accidental globals (well, package variables in Perl terms). This can't happen in Python as bare assignments default to local declaration, and bare unassigned symbols result in an exception.

    (There is still the case where users accidentally try to write-through to a global without declaring it with a ‘global’ statement, causing either an accidental local or, more often, an UnboundLocalError. This tends to be learned fairly quickly, but it is an arguable case where having to declare your locals could help. Although few experienced Python programmers would accept the readability burden.)

    Other language and library changes that do not involve syntax are handled through the warnings system.

提交回复
热议问题