I am trying to make my python2.x-Code compatible with both 2.7 and 3.x. Currently I am stuck at some Code in Pmw.py (from python megawidgets). Have a a look at the first three entries of this dictionary:
_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),
}
The first two entries contain “string.atol”. My questions are:
-
In the python docs atol is introduced as a function (
string.atol(s[, base])) , so there should be parentheses, which are missing here. So how is this syntax to be understood? -
In python 3.2 this code raises the error:
'numeric' : (numericvalidator, string.atol), AttributeError: 'module' object has no attribute 'atol'I already tried replacing the three occurences of “atol” with long, like suggested in the python docs, but that just raised the error:
'numeric' : (numericvalidator, string.long), AttributeError: 'module' object has no attribute 'long'As I don’t even understand the syntax, I’m quite helpless about what to try next. How is this code to be fixed, so that it works both in python 2.7 and 3.x?
Hope you can help me on that one.
1:
string.atolis the function itself: functions are first-class objects in python. The parentheses are only used for a call.2: I think you must have misread. long doesn’t live in string, but there isn’t a long in Python 3 anyway. That’s a relic of when Python distinguished between small integers and long integers in ways that could be seen from userspace. (That’s what the “L” on the end of 43698L above means.)
Simply use int, i.e.