Python print and types selection

mark.seagoe at gmail.com mark.seagoe at gmail.com
Sat Mar 28 09:43:12 EDT 2009


On Mar 27, 4:00 pm, Miles <semantic... at gmail.com> wrote:
> On Fri, Mar 27, 2009 at 6:56 PM, Gabriel Genellina wrote:
> > En Fri, 27 Mar 2009 18:43:16 -0300, <mark.sea... at gmail.com> escribió:
> >> Python print recognizes the local constant "dog", but it goes and
> >> fetches the __int__ type from my object-based class, even though it's
> >> value is a long.  Then Python print doesn't expect a long to come back
> >> and bombs out.  I don't want to force the main program to cast the
> >> long value getting returned.  I know Python print can print a long,
> >> but how can I make it select the __long__ instead of the __int__ ?
>
> > This bug was corrected in version 2.6 - see
> >http://bugs.python.org/issue1742669
> > If you have to stay with 2.5 I'm afraid any solution would require to modify
> > your code:
>
> > -- put long() around those arguments
> > -- "%s" % format_hex(val) (where format_hex does the long conversion)
> > -- redefine __str__ and use %s
>
> Or make your class a subclass of long.
>
> -Miles- Hide quoted text -
>
> - Show quoted text -

It appears that if I make the class a subclass of long...
class bignumber(long):
    def __init__(self, initval):
        self.val = initval

Then if I make a new class of subclass of bignumber...
class myclass(bignumber):
    def __init__(self, another_custom_class)
        bignumber.__init__(self, 0)
        do some stuff with another_custom_class

When I try to use this, I get an error sort of like this:
"TypeError: long() argument must be a string or a number, not
[whatever type another_custom_class is]"

Would paste code here but it's pretty extensive, and hopefully this
conveys the idea.  For me upgrading Python version is at this time not
an option, because I work for a very large company and they're all
using Python 2.5.3.

So then is it required that I stuff a long as the first arg of
myclass, or is there a way around this?  I read another post and
followed advice to add the __new__() method, actually looking the same
as __init__(), but then it later bombs out when the main routine tries
to print from this line:
print 'dog val = 0x%016X' % dog
saying "TypeError: int argument required", which is worse than it was
originally.

I probably need some tutorial on internals of Python, huh?

Thanks!




More information about the Python-list mailing list