Is a list an instance of a class?

Dan Bishop danb_83 at yahoo.com
Mon Nov 15 13:01:05 CET 2004


Kent Johnson <kent3737 at yahoo.com> wrote in message news:<4197c013$1_1 at newspeer2.tds.net>...
> In the Python tutorial section 9.1, it says, "the word ``object'' in 
> Python does not necessarily mean a class instance. Like C++ and 
> Modula-3, and unlike Smalltalk, not all types in Python are classes: the 
> basic built-in types like integers and lists are not, and even somewhat 
> more exotic types like files aren't."
> 
> Is this still correct or was it made obsolete by Python 2.2? Lists and 
> files have __class__ attributes, at least:

For all practical purposes, it's obsolete.  You can inherit from "non-classes" now.

>>> class TallyMarks(int):
...    def __repr__(self):
...       return 'I' * self
...
>>> TallyMarks(12)
IIIIIIIIIIII


>  >>> [].__class__
>  <type 'list'>
>  >>> f = open('ThreadQueue.py')
>  >>> f.__class__
> <type 'file'>
> 
> So do integers and floats, though you have to ask nicely for an int:
>  >>> 1.__class__
>    File "<stdin>", line 1
>      1.__class__
>                ^
> SyntaxError: invalid syntax
>  >>> x=1
>  >>> x.__class__
>  <type 'int'>

You don't have to ask *that* nicely.

>>> 1 .__class__  # notice the space
<type 'int'>



More information about the Python-list mailing list