1. <tfoot id='t7A4S'></tfoot>
    • <bdo id='t7A4S'></bdo><ul id='t7A4S'></ul>
  2. <legend id='t7A4S'><style id='t7A4S'><dir id='t7A4S'><q id='t7A4S'></q></dir></style></legend>

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

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

      如何在 Python 中创建常量?

      How do I create a constant in Python?(如何在 Python 中创建常量?)
        <tbody id='5U8BF'></tbody>

      <small id='5U8BF'></small><noframes id='5U8BF'>

      <legend id='5U8BF'><style id='5U8BF'><dir id='5U8BF'><q id='5U8BF'></q></dir></style></legend>

                <bdo id='5U8BF'></bdo><ul id='5U8BF'></ul>

              • <tfoot id='5U8BF'></tfoot>
                <i id='5U8BF'><tr id='5U8BF'><dt id='5U8BF'><q id='5U8BF'><span id='5U8BF'><b id='5U8BF'><form id='5U8BF'><ins id='5U8BF'></ins><ul id='5U8BF'></ul><sub id='5U8BF'></sub></form><legend id='5U8BF'></legend><bdo id='5U8BF'><pre id='5U8BF'><center id='5U8BF'></center></pre></bdo></b><th id='5U8BF'></th></span></q></dt></tr></i><div id='5U8BF'><tfoot id='5U8BF'></tfoot><dl id='5U8BF'><fieldset id='5U8BF'></fieldset></dl></div>
                本文介绍了如何在 Python 中创建常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有办法在 Python 中声明一个常量?在 Java 中,我们可以通过这种方式创建常量值:

                Is there a way to declare a constant in Python? In Java we can create constant values in this manner:

                public static final String CONST_NAME = "Name";
                

                上述Java常量声明在Python中的等价物是什么?

                What is the equivalent of the above Java constant declaration in Python?

                推荐答案

                没有没有.您不能在 Python 中将变量或值声明为常量.只是不要改变它.

                No there is not. You cannot declare a variable or value as constant in Python. Just don't change it.

                如果您在课堂上,则相当于:

                If you are in a class, the equivalent would be:

                class Foo(object):
                    CONST_NAME = "Name"
                

                如果没有,那只是

                CONST_NAME = "Name"
                

                但您可能想看看代码片段 Python 中的常量 by Alex Martelli.

                But you might want to have a look at the code snippet Constants in Python by Alex Martelli.

                从 Python 3.8 开始,有一个 typing.Final 变量注释将告诉静态类型检查器(如 mypy)不应重新分配您的变量.这是最接近 Java 的 final 的等价物.但是,它实际上并不能阻止重新分配:

                As of Python 3.8, there's a typing.Final variable annotation that will tell static type checkers (like mypy) that your variable shouldn't be reassigned. This is the closest equivalent to Java's final. However, it does not actually prevent reassignment:

                from typing import Final
                
                a: Final = 1
                
                # Executes fine, but mypy will report an error if you run mypy on this:
                a = 2
                

                这篇关于如何在 Python 中创建常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 库)
              • <tfoot id='ogy0S'></tfoot>
                <i id='ogy0S'><tr id='ogy0S'><dt id='ogy0S'><q id='ogy0S'><span id='ogy0S'><b id='ogy0S'><form id='ogy0S'><ins id='ogy0S'></ins><ul id='ogy0S'></ul><sub id='ogy0S'></sub></form><legend id='ogy0S'></legend><bdo id='ogy0S'><pre id='ogy0S'><center id='ogy0S'></center></pre></bdo></b><th id='ogy0S'></th></span></q></dt></tr></i><div id='ogy0S'><tfoot id='ogy0S'></tfoot><dl id='ogy0S'><fieldset id='ogy0S'></fieldset></dl></div>
                  <bdo id='ogy0S'></bdo><ul id='ogy0S'></ul>
                    • <small id='ogy0S'></small><noframes id='ogy0S'>

                        <tbody id='ogy0S'></tbody>
                    • <legend id='ogy0S'><style id='ogy0S'><dir id='ogy0S'><q id='ogy0S'></q></dir></style></legend>