max_client =
100max_connection =
1000
connection_timeout =
600
import sys
"""該類定義了乙個方法__setattr()__,和乙個異常consterror, consterror類繼承
自類typeerror. 通過呼叫類自帶的字典__dict__, 判斷定義的常量是否包含在字典
中。如果字典中包含此變數,將丟擲異常,否則,給新建立的常量賦值。
最後兩行**的作用是把const類註冊到sys.modules這個全域性字典中。
"""class
_const
:# 自定義異常處理
class
consterror
(permissionerror)
:pass
class
constcaseerror
(consterror)
:pass
# 重寫 __setattr__() 方法
def__setattr__
(self, name, value)
:if name in self.__dict__:
# 已包含該常量,不能二次賦值
raise self.consterror(
"can't change const "
.format
(name))if
not name.isupper():
# 所有的字母需要大寫
raise self.constcaseerror(
"const name is not all uppercase"
.format
(name)
) self.__dict__[name]
= value
# 將系統載入的模組列表中的 constant 替換為 _const() 例項
sys.modules[__name__]
= _const(
)
import constant
constant.value =
5constant.value =
4# consterror
constant.value =
1# constcaseerror
python語言常量 Python 定義常量
python python開發 python語言 python 定義常量 常量在編寫程式的時候,一旦設定就不能再進行變動,常量一種約定俗成的方式,所有字母大寫並用下劃線分隔單詞的方式 如max value,out time等 但是python沒有提供設定常量用法,需要自己自定義類實現常量功能。自定義...
Python定義常量
i 訪問字典的元素使用dobj.get key somethingelse 如果對應key值元素不存在,你將會得到somethingelse值,例如 not found 不要使用dobj key 因為如果key對應元素不存在,則會產生keyerror異常,這樣必須使用try except來封裝 ii...
python定義常量
coding utf 8 filename const.py class const class consterror typeerror pass def setattr self,name,value if self.dict has key name raise self.consterror...