[Edu-sig] Consistency question

Kirby Urner urnerk at qwest.net
Wed Sep 24 22:06:30 EDT 2003


So I'm wondering how one gets a handle on int-the-builtin-function versus
int the integer type (is there such?).

For example:

 >>> type(__builtins__.iter)
 <type 'builtin_function_or_method'>

 >>> type(__builtins__.issubclass)
 <type 'builtin_function_or_method'>

but

 >>> type(__builtins__.int)
 <type 'type'>

It would seem there'd be a builtin_function_or_method named int, in addition
to the int type, but I can't seem to get at it using dir() or type().  Is
there a way to bind the built-in int function to a name?

Kirby

> -----Original Message-----
> From: edu-sig-bounces at python.org [mailto:edu-sig-bounces at python.org] On
> Behalf Of Guido van Rossum
> Sent: Wednesday, September 24, 2003 5:18 PM
> To: Kirby Urner
> Cc: edu-sig at python.org
> Subject: Re: [Edu-sig] Consistency question
> 
> > My understanding is that int(5.0) actually triggers __int__ -- a method
> > inherited by all floats.
> >
> > When I go dir(5.0), I see __int__ listed, and I can go:
> >
> >  >>> 5.0 .__int__()
> >  5
> >
> > So far so good.
> >
> > But then I can also go int('123') and get back 123.  So why isn't
> __int__ a
> > method of string objects?
> >
> >  >>> '123' .__int__()
> >
> > gets me an error, and dir('') doesn't show __int__ as being among
> string's
> > methods.
> >
> > Why?
> >
> > I'd like to clear up this confusion because I'm writing some tutorial
> > materials.  What I don't understand, I can't rightly explain.
> 
> int() special-cases various argument types.  It only calls __int__
> when none of the special cases apply.  An argument of type str is a
> special case (as is an argument of type unicode) because in that case,
> and *only* that case, the optional second 'base' argument is accepted.
> 
> (If you really want to know more, RTSL. :-)
> 
> --Guido van Rossum (home page: http://www.python.org/~guido/)







More information about the Edu-sig mailing list