1. <tfoot id='VPhCa'></tfoot>
        <bdo id='VPhCa'></bdo><ul id='VPhCa'></ul>

      <small id='VPhCa'></small><noframes id='VPhCa'>

      <i id='VPhCa'><tr id='VPhCa'><dt id='VPhCa'><q id='VPhCa'><span id='VPhCa'><b id='VPhCa'><form id='VPhCa'><ins id='VPhCa'></ins><ul id='VPhCa'></ul><sub id='VPhCa'></sub></form><legend id='VPhCa'></legend><bdo id='VPhCa'><pre id='VPhCa'><center id='VPhCa'></center></pre></bdo></b><th id='VPhCa'></th></span></q></dt></tr></i><div id='VPhCa'><tfoot id='VPhCa'></tfoot><dl id='VPhCa'><fieldset id='VPhCa'></fieldset></dl></div>

        <legend id='VPhCa'><style id='VPhCa'><dir id='VPhCa'><q id='VPhCa'></q></dir></style></legend>
      1. 为什么 Python 中没有“const"?

        Why no #39;const#39; in Python?(为什么 Python 中没有“const?)
          <bdo id='Uk4Dx'></bdo><ul id='Uk4Dx'></ul>
        • <small id='Uk4Dx'></small><noframes id='Uk4Dx'>

            <tbody id='Uk4Dx'></tbody>

              • <i id='Uk4Dx'><tr id='Uk4Dx'><dt id='Uk4Dx'><q id='Uk4Dx'><span id='Uk4Dx'><b id='Uk4Dx'><form id='Uk4Dx'><ins id='Uk4Dx'></ins><ul id='Uk4Dx'></ul><sub id='Uk4Dx'></sub></form><legend id='Uk4Dx'></legend><bdo id='Uk4Dx'><pre id='Uk4Dx'><center id='Uk4Dx'></center></pre></bdo></b><th id='Uk4Dx'></th></span></q></dt></tr></i><div id='Uk4Dx'><tfoot id='Uk4Dx'></tfoot><dl id='Uk4Dx'><fieldset id='Uk4Dx'></fieldset></dl></div>
                • <tfoot id='Uk4Dx'></tfoot>
                • <legend id='Uk4Dx'><style id='Uk4Dx'><dir id='Uk4Dx'><q id='Uk4Dx'></q></dir></style></legend>
                  本文介绍了为什么 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...

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    <bdo id='fbFtq'></bdo><ul id='fbFtq'></ul>

                      <tfoot id='fbFtq'></tfoot>

                        <small id='fbFtq'></small><noframes id='fbFtq'>

                        • <legend id='fbFtq'><style id='fbFtq'><dir id='fbFtq'><q id='fbFtq'></q></dir></style></legend>
                              <tbody id='fbFtq'></tbody>
                            <i id='fbFtq'><tr id='fbFtq'><dt id='fbFtq'><q id='fbFtq'><span id='fbFtq'><b id='fbFtq'><form id='fbFtq'><ins id='fbFtq'></ins><ul id='fbFtq'></ul><sub id='fbFtq'></sub></form><legend id='fbFtq'></legend><bdo id='fbFtq'><pre id='fbFtq'><center id='fbFtq'></center></pre></bdo></b><th id='fbFtq'></th></span></q></dt></tr></i><div id='fbFtq'><tfoot id='fbFtq'></tfoot><dl id='fbFtq'><fieldset id='fbFtq'></fieldset></dl></div>