python调用其他文件的常量,将一长串常量导入到Python文件

在Python中没有类似C预处理器的#define语句。但可以通过创建一个包含常量的.py文件并导入来实现。例如,在Constants.py中定义常量,然后在其他如myExample.py的文件中通过`import Constants`来使用这些常量。如果遇到`NameError`,可能是因为导入未正确执行。正确的做法是确保常量文件被正确导入,并使用如`Constants.MY_CONSTANT_ONE`的方式引用。
摘要由CSDN通过智能技术生成

In Python, is there an analogue of the C preprocessor statement such as?:

#define MY_CONSTANT 50

Also, I have a large list of constants I'd like to import to several classes. Is there an analogue of declaring the constants as a long sequence of statements like the above in a .py file and importing it to another .py file?

Edit.

The file Constants.py reads:

#!/usr/bin/env python

# encoding: utf-8

"""

Constants.py

"""

MY_CONSTANT_ONE = 50

MY_CONSTANT_TWO = 51

And myExample.py reads:

#!/usr/bin/env python

# encoding: utf-8

"""

myExample.py

"""

import sys

import os

import Constants

class myExample:

def __init__(self):

self.someValueOne = Constan

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值