如何在python中设置全局常量变量 [英] how to set global const variables in python

查看:1081
本文介绍了如何在python中设置全局常量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含各种类别和功能的解决方案,所有这些解决方案都需要获得一些全球的支持者才能够正常工作。由于python中没有 const ,你认为最好的做法是设置一种全局的支持者。

  global const g = 9.8 

所以我正在寻找一种



编辑:如何:

  class Const ):
@staticmethod
def gravity():
return 9.8

print'gravity:',Const.gravity()

解决方案

Python中的常量。如果你发现某种手段做到这一点,你只会混淆每个人。



要做到这一点,通常你应该只有一个模块 - globals.py 例如,您可以随处导入需要的文件。


I am building a solution with various classes and functions all of which need access to some global consants to be able to work appropriately. As there is no const in python, what would you consider best practice to set a kind of global consants.

global const g = 9.8 

So I am looking for a kind of the above

edit: How about:

class Const():
    @staticmethod
    def gravity():
        return 9.8

print 'gravity: ', Const.gravity()

?

解决方案

You cannot define constants in Python. If you find some sort of hack to do it, you would just confuse everyone.

To do that sort of thing, usually you should just have a module - globals.py for example that you import everywhere that you need it

这篇关于如何在python中设置全局常量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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