Minor 2.0 incompatibility

Hamish Lawson hamish_lawson at yahoo.co.uk
Wed Oct 25 12:21:11 EDT 2000


I was surpised when you said it used to work before, but others in this
thread have given an explanation of that (which I've still to digest).
Usually a call like self.fromString(s) would result in self and s being
passed as parameters. You can see this in the modified version below of
your example:

    |def myatoi(s, base=10):
    |    return "Converting '%s' using base %s" % (s, base)
    |
    |class A:
    |   fromString = myatoi
    |   def go(self, s):
    |       print self.fromString(s)
    |
    |a = A()
    |a.go('23')

This gives as output

    Converting '<__main__.A instance at 0079907C>' using base 23

demonstrating that self is being passed as the s parameter and the
string '23' is being passed as the base, which isn't what you intend.
This is why you got the traceback error that you did, since string.atoi
expects an integer as the base but instead was getting your string
parameter. If I understand the upshot of Andrew Dalke's explanation
correctly, the different type that string.atoi used to have in Python
1.5.2 meant that self didn't get passed to it.

Hamish Lawson

> 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' )
>
> giving output...
>
>  | tcc2:timd $ /opt/python/python1.5.2/bin/python test.py
>  | 10
>  | tcc2:timd $ /opt/python/python2.0/bin/python test.py
>  | Traceback (most recent call last):
>  |   File "test.py", line 10, in ?
>  |     a.go( '10' )
>  |   File "test.py", line 6, in go
>  |     print self.fromString(s)
>  |   File "/opt/python/python2.0/lib/python2.0/string.py", line 215,
in atoi
>  |     return _int(s, base)
>  | TypeError: an integer is required
>  | tcc2:timd $
>
> Presumably, defining assigning string.atoi directly to fromString is
> dubious, and the following works ok...
>
>  | import string
>  |
>  | class A:
>  |     def fromString(self, s): return  string.atoi(s)
>  |
>  |     def go(self, s):
>  |         print self.fromString(s)
>  |
>  | a = A()
>  | a.go( '10' )
>
> It's an easy enough fix, once I worked out what to do. But what is
> actually going on here? Something seems to have changed with the way
> that atoi now works, and somehow it worked by fluke before. Either
> way, the traceback is a bit cryptic.
>
> Comments?
>
> Tim
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list