Everything is an object in python - object class and type class
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jun 2 02:23:11 EDT 2015
On Tuesday 02 June 2015 11:15, TheDoctor wrote:
> A type is not an object.
Yes it is.
py> isinstance(type, object)
True
py> isinstance(int, object)
True
> You see it as one, because you are MENTALLY
> lexing your own code on the screen.
No, I see it as one, because it is one.
> But python does not define a type.
Of course it does.
py> isinstance(int, type) # int is a type
True
py> isinstance(42, type) # 42 is not a type
False
> It defines certain primitives, like strings and integers. But it doesn't
> define what an OBJECT is.
In one sense, it does: something is an object if it is an instance of type
"object".
In another sense, it doesn't, because the definition of "object" is bigger
than Python and exists outside of Python. Python had objects before it had a
built-in type called "object".
> So you and everyone else are beating around the
> bush trying to define something that the LANGUAGE ITSELF doesn't define.
Incorrect.
> In Python 2.7 type(type) is type (not object), but isinstance(type,
> object) is true -- so it is ambiguous, even contradictory, in the
> language.
No, it is not ambiguous nor contradictory. It just means that types
(classes), include type itself, are *both* types AND objects. Which is the
whole point of saying that everything is an object! This is not true in all
Object Oriented languages, for instance it is not true in Java, but it is
true in Python.
Notice that we do NOT say that "everything is a type":
py> isinstance(42, type)
False
In Python, all types (classes) are objects, but not all objects (instances)
are types.
--
Steve
More information about the Python-list
mailing list