Minor 2.0 incompatibility

Andrew Dalke dalke at acm.org
Wed Oct 25 01:37:07 EDT 2000


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

http://x55.deja.com/[ST_rn=ps]/getdoc.xp?
AN=639198917&CONTEXT=972451795.658374665&hitnum=3

(The documentation has been fixed since then to reflect the implementation.)

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list