Is a list an instance of a class?

K

Kent Johnson

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:
>>> [].__class__
>>> f = open('ThreadQueue.py')
>>> f.__class__
<type 'file'>

So do integers and floats, though you have to ask nicely for an int: File "<stdin>", line 1
1.__class__
^
SyntaxError: invalid syntax<type 'float'>

Kent
 
D

Dan Bishop

Kent Johnson said:
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.
.... def __repr__(self):
.... return 'I' * self
....
IIIIIIIIIIII

f = open('ThreadQueue.py')
f.__class__
<type 'file'>

So do integers and floats, though you have to ask nicely for an int:File "<stdin>", line 1
1.__class__
^
SyntaxError: invalid syntax<type 'int'>

You don't have to ask *that* nicely.
<type 'int'>
 
S

Steven Bethard

Dan Bishop said:
You don't have to ask *that* nicely.

<type 'int'>

Why does this work? Or, perhaps my real question is why *doesn't* it work
without the space? Pointers to the appropriate point in the docs would be
fine... I assume it's something about making parsing easier...?

Thanks,

Steve
 
E

Erik Max Francis

Steven said:
Why does this work? Or, perhaps my real question is why *doesn't* it work
without the space? Pointers to the appropriate point in the docs would be
fine... I assume it's something about making parsing easier...?

It's because otherwise it's confusing it for a floating point literal.
All you need to do is put a break so that the parser can't get confused.
1 ._class__ or (1).__class__ will work just fine.
 
A

Alex Martelli

Steven Bethard said:
Why does this work? Or, perhaps my real question is why *doesn't* it work
without the space? Pointers to the appropriate point in the docs would be
fine... I assume it's something about making parsing easier...?

It's to make lexing more predictable: it's known as maximal-much lexing
(without backtracking: backtracking in lexical analysis would really but
really be bad). '1.' (no space) is a token (stands for a float
literal). So, '1.__class__' is two tokens: float '1.' followed by
identified '__class__'.


Alex
 
S

Steve Holden

Steven said:
Why does this work? Or, perhaps my real question is why *doesn't* it work
without the space? Pointers to the appropriate point in the docs would be
fine... I assume it's something about making parsing easier...?

Thanks,

Steve
Yup, it's because a dot immediately following an integer would be parsed
(strictly "lexed", I suppose) as a floating point constant,m aking the
__class__ a syntax error.

regards
Steve
 
R

Russell Blau

Steve Holden said:
Yup, it's because a dot immediately following an integer would be parsed
(strictly "lexed", I suppose) as a floating point constant,m aking the
__class__ a syntax error.

regards
Steve

And, in case anyone was wondering (as I was), the following works, too,
despite the fact that it looks incredibly odd:
<type 'float'>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top