我想讓我的python2.x-Code兼容2.7和3.x.目前我被困在Pmw.py的一些代碼中(來自python megawidgets)。有一個看看這個字典的前三項:如何找到python3中string.atol的兼容替代方案
_standardValidators = {
'numeric' : (numericvalidator, string.atol),
'integer' : (integervalidator, string.atol),
'hexadecimal' : (hexadecimalvalidator, lambda s: string.atol(s, 16)),
'real' : (realvalidator, Pmw.stringtoreal),
'alphabetic' : (alphabeticvalidator, len),
'alphanumeric' : (alphanumericvalidator, len),
'time' : (timevalidator, Pmw.timestringtoseconds),
'date' : (datevalidator, Pmw.datestringtojdn),
}
前兩個條目包含「string.atol」。我的問題是:
在Python文檔蒂被引入作爲一個函數(
string.atol(s[, base])
),所以應該有括號,這是在這裏失蹤。那麼如何理解這個語法呢?在蟒蛇3.2這段代碼引發錯誤:
'numeric' : (numericvalidator, string.atol), AttributeError: 'module' object has no attribute 'atol'
我已經嘗試過長替換的「蒂」三個OCCURENCES,像Python文檔建議,但只是引發的錯誤:
'numeric' : (numericvalidator, string.long), AttributeError: 'module' object has no attribute 'long'
因爲我甚至不理解語法,所以我對接下來要嘗試的東西感到無奈。這段代碼是如何修復的,以便它能夠在python 2.7和3.x中使用?
希望你能幫助我。
「在蟒蛇3.2這段代碼引發錯誤:」 - 你忘了說的錯誤是什麼是。 – 2012-01-28 04:46:48
@LennartRegebro:他確實說了什麼是錯誤信息,雖然 - 他粘貼了AttributeError。格式化有一些不足之處,但這是他的第一個。 :^) – DSM 2012-01-28 05:05:56
@DSM對,你是我的壞人。格式不知何故,我的眼睛只是跳過它, – 2012-01-28 07:43:16