Minor 2.0 incompatibility
xyzmats at laplaza.org
Wed Oct 25 14:45:00 CEST 2000
On Tue, 24 Oct 2000 23:37:07 -0600, "Andrew Dalke" <dalke at acm.org>
>Michael Hudson wrote:
>>Timothy Docker <timd at macquarie.com.au> writes:
>>> The following code works under 1.5.2 but not under 2.0...
>>> | import string
>>> | class A:
>>> | fromString = string.atoi
>>> | def go(self, s):
>>> | print self.fromString(s)
>>> | a = A()
>>> | a.go( '10' )
>>What's happened is that string.atoi is now a Python function, when it
>>used to be of builtin-function-or-method type. No time for more
>>explaination now, I've got lectures to get too...
>To elaborate, pure Python functions in class scope are considered member
>functions, even if they aren't defined that way. For the spec, see
>"User-defined methods" in http://www.python.org/doc/current/ref/types.html .
>} User-defined method objects are created in two ways: when getting an
>} attribute of a class that is a user-defined function object, ...
>The reason it changed for you is because string.atoi used to be a
>builtin-function-or-method but is now a user-defined function so is
>treated as a method call instead of a function.
>I personally think the behaviour is unexpected and inappropriate; see
>the thread titled "unexpected behaviour in class variables", as in
Hmmm. I tripped over something like this.
The /real/ story is, atoi() atol() and atof() are deprecated as of
2.0, in favor of using int() long() and float().
These are not available as string methods, but remain in the string
module for compatibility (sort of...).
In other words:
str = "100.25"
num = string.atof(str) <-- maybe, but float(str) is perferred
num = str.atof <-- sorry, no such method!
(Anti-spam stuff: to reply remove the "xyz" from the
address xyzmats at laplaza.org. Not that it helps much...)
More information about the Python-list