Integers have docstring for int()

Peter Abel PeterAbel at
Thu Aug 12 13:27:18 CEST 2004

Christian Tismer <tismer at> wrote in message news:<mailman.1515.1092243911.5135.python-list at>...
> Skip Montanaro wrote:
> >     Andrew> I accidentally discovered that all the basic types in Python
> >     Andrew> have docstrings that describe the functions to create them. For
> >     Andrew> integers, you get:
> >     ...
> > 
> >     Andrew> This seems a little unexpected to me; is there any particular
> >     Andrew> reason for this behaviour?
> > 
> > Why should this be unexpected?  If you execute
> > 
> >     print int.__doc__
> > 
> > you get its docstring.  Since the __doc__ attribute is attached to the class
> > it will be found when the search starts at an instance of the class as well.
> Sure, this is why. But I can understand the feeling a little
> bit, because the docstring describes what a __call__ to the
> type object does, although the instance is not even callable.
> This may origin in the fact, that int and str have been just
> functions a short while back, and the docstrings mainly describe
> that function call. The docs are less representative
> for int being the whole class. One would perhaps expect
> a short description of what an int can do.
> On the other hand, int() perceived as a function still needs
> its documentation as it is now.
> Like we have the __call__ attribute of a type, which describes
> the call of the *instance*, and this differs completely from
> how the type is called, would it make sense to change the
> __doc__ lookup of types vs. instances in some way?
> The current doc strings would probably better fit into int.__new__
> or int.__init__, while the class __doc__ might talk about instances.
> (although I even think no documentation would be better than
>   the existing docs in the case of strings and ints, which
>   show up in some object browsers, and you really don't want to
>   read long musings about strings while inspecting a string value
>   in the PythonWin debugger).
> ciao - chris

The following seems to be unaccustomed, but it's good Python:

>>> class myInt(int):
... 	"""Instance of myInt"""
... 	def __init__(self,val):
... 		self=int(val)
>>> i=myInt(10)
>>> type(i)
<class '__main__.myInt'>
>>> i.__doc__
'Instance of myInt'
>>> print myInt.__doc__
Instance of myInt
>>> j=2
>>> print j.__doc__
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
>>> type(j)
<type 'int'>
>>> j+i

At my knowledge, since Python 2.1?? or 2.2?? basic types are like or are classes
where you can inherit from. So their behavior is class-like.


More information about the Python-list mailing list