我推薦使用json庫,它允許你以可讀和可修改的格式存儲你的常量值。
使用@ Daniel's常量類繼承自float並添加您的自定義屬性,您可以一次將所有常量加載到新的Constants
對象中。
然後,您可以獲取這些屬性作爲c.R來訪問該值。
完整的文件:
#!/usr/bin/env python
import json
class Constant(float):
def __new__(cls, value):
self = float.__new__(cls, value["value"]) # KeyError if missing "value"
self.units = value.get("units", None)
self.doc = value.get("doc", None)
return self
class Constants():
# load the json file into a dictionary of Constant objects
def __init__(self):
with open("constants.json") as fh:
json_object = json.load(fh)
# create a new dictionary
self.constants_dict = {}
for constant in json_object.keys():
# put each Constant into it
self.constants_dict[constant] = Constant(json_object[constant])
# try to get the requested attribute
def __getattr__(self, name):
# missing keys are returned None, use self.constants_dict[name]
# if you want to raise a KeyError instead
return self.constants_dict.get(name, None)
c = Constants()
print c.R # 287.102
print c.R.doc # ideal gas constant
print c.R + 5 # 292.102
print c.F.units # C mol-1
print c.missing # None
例constants.json:
{
"R": {
"value": 287.102,
"units": "J/(kg K)",
"doc": "ideal gas constant"
},
"F": {
"value": 96485.33,
"units": "C mol-1",
"doc": "Faraday contant"
}
}
來源
2017-01-07 15:19:44
ti7
我敢肯定這是不可能的 - cR意味着它在你想訪問attribut的下一次調用中浮動浮點數,這是簡單的類型...你應該考慮使用cR()或cRval,這將使它成爲可能 – MacHala
你應該看看[這個scipy模塊](https://docs.scipy.org/doc/ scipy/reference/constants.html) – Lucas
可以使用子類或使用委託並實現所有特殊方法。哪個選擇最好取決於(雖然我會傾向於代表團以最少的驚喜)。 – Bakuriu