为什么在Python中没有“ const”? [英] Why no 'const' in Python?

查看:561
本文介绍了为什么在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.

前一个是静态类型的。后者是 dynamic

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 ,< a href = https://pypi.python.org/pypi/pyflakes> 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...

这篇关于为什么在Python中没有“ const”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆