为什么 Python 中没有“const"?
问题描述
我来自 C 背景,正在学习 Python.缺乏明确的类型安全令人不安,但我已经习惯了.面对动态语言的所有优势,缺乏内置的基于契约的编程(纯抽象类、接口)是需要习惯的.
I come from C background and am learning Python. The lack of explicit type-safety is disturbing, but I am getting used to it. The lack of built-in contract-based programming (pure abstract classes, interfaces) is something to get used to, in the face of all the advantages of a dynamic language.
但是,无法请求 const-cortectness 让我抓狂!为什么 Python 中没有常量?为什么不鼓励类级常量?
However, the inability to request const-cortectness is driving me crazy! Why are there no constants in Python? Why are class-level constants discouraged?
解决方案
C和Python分属两种不同的语言.
C and Python belongs to two different classes of languages.
前者是静态类型的.后者是动态的.
The former one is statically typed. The latter is dynamic.
在静态类型语言中,类型检查器能够推断每个表达式的类型,并在编译"阶段检查它是否与给定声明匹配.
In a statically typed language, the type checker is able to infer the type of each expression and check if this match the given declaration during the "compilation" phase.
在动态类型语言中,所需的类型信息直到运行时才可用.并且表达式的类型可能因一次运行而异.当然,您可以在程序执行期间添加类型检查.这不是在 Python 中做出的选择.这具有允许鸭子打字"的优势.缺点是解释器无法检查类型的正确性.
In a dynamically typed language, the required type information is not available until run-time. And the type of an expression may vary from one run to an other. Of course, you could add type checking during program execution. This is not the choice made in Python. This has for advantage to allow "duck typing". The drawback is the interpreter is not able to check for type correctness.
关于 const 关键字.这是一个类型修饰符.限制允许使用的变量(有时修改允许的编译器优化).在运行时检查动态语言的效率似乎很低.在第一次分析中,这意味着要检查一个变量是否为 const 或不是每个做作.这可以进行优化,但即便如此,这样做是否值得?
Concerning the const keyword. This is a type modifier. Restricting the allowed use of a variable (and sometime modifying allowed compiler optimization). It seems quite inefficient to check that at run-time for a dynamic language. At first analysis, that would imply to check if a variable is const or not for each affectation. This could be optimized, but even so, does it worth the benefit?
除了技术方面,不要忘记每种语言都有自己的理念.在 Python 中,通常的选择是支持约定"而不是限制".例如,常量应该全部大写.没有技术强制执行.这只是一个约定.如果您遵循它,您的程序将按照其他程序员"的预期运行.如果您决定修改常量",Python 不会抱怨.但是你应该觉得你在做错事".你打破了约定.也许你有你这样做的理由.也许你不应该有.你的责任.
Beyond technical aspects, don't forget that each language has its own philosophy. In Python the usual choice is to favor "convention" instead of "restriction". As an example, constant should be spelled in all caps. There is no technical enforcement of that. It is just a convention. If you follow it, your program will behave as expected by "other programmers". If you decide to modify a "constant", Python won't complain. But you should feel like your are doing "something wrong". You break a convention. Maybe you have your reasons for doing so. Maybe you shouldn't have. Your responsibility.
最后一点,在动态语言中,程序的正确性"更多地是单元测试的责任,而不是编译器的责任.如果你真的很难做到这一步,你会发现一些代码检查器".这些是 PyLint、PyChecker、PyFlakes...
As a final note, in dynamic languages, the "correctness" of a program is much more of the responsibility of your unit testings, than in the hand of the compiler. If you really have difficulties to made the step, you will find around some "code checkers". Those are PyLint, PyChecker, PyFlakes...
相关文章