Reputation: 99408
When I read Python in a Nutshell, I saw a "type object", "class object", "function object", "module object", ....
Does a "xxx object" mean an instance of type "xxx"?
For example,
does a type object mean an instance of type type
? For example, is int
a type object? is a specific class a type object?
does a class object mean an instance of what? Is a specific class a class object? For example,
A class is a Python object with several characteristics:
• You can call a class object as if it were a function. The call, often known as instantiation, returns an object known as an instance of the class; the class is known as the type of the instance.
• A class can inherit from other classes, meaning it delegates to other class objects the lookup of attributes that are not found in the class itself.
does a function object mean an instance of types.FunctionType
? Is a specific function a function object?
does a module object mean an instance of types.ModuleType
? Is a specific module a module object?
is there an int object? If yes, does it mean an integer value (e.g. 12
) of type int
?
Thanks.
Upvotes: 1
Views: 119
Reputation: 160377
Does a type object mean an instance of type
type
? For example, isint
a type object? Ιs a specific class a type object?
Yes, a "type" object means an object of type "type", ergo an instance of that type. Information on what type something is can always be checked with isinstance
. To answer your second question, is int
an object of type type
:
>>> isinstance(int, type) # True
Are custom classes type objects:
>>> class Spammy:
... pass
>>> isinstance(Spammy, type)
True
Does a class object mean an instance of what? Is a specific class a class object?
The use of class object is interchangeable with the term type
object after the Unification of types and classes in Python 2.2. The term 'classes' is mainly used to refer to user defined classes (the ones you create with the class
statement) but it really means the same thing.
This did make a difference in Python 2 that has old style classes. Not inheriting from object in a class definition gave an object of type classobj
:
>>> class Foo:
... pass
>>> type(Foo)
<type 'classobj'>
This just doesn't exist in Python 3 where all classes are objects of type type
.
Does a function object mean an instance of
types.FunctionType
? Is a specific function a function object?Does a module object mean an instance of
types.ModuleType
? Is a specific module a module object?
Yes to all of these:
>>> def foo(): pass
>>> type(foo)
function
>>> import collections
>>> type(collections)
module
Is there an
int
object? If yes, does it mean an integer value (e.g.12
) of typeint
?
There is an int
type, which is a type object and an int
object, which is an object of type int
. An example of that is 12
since:
>>> type(12)
int
While, the same thing on int
yields:
>>> type(int)
type
Upvotes: 4
Reputation: 3196
See the documentation of type()
:
With one argument, return the type of an object. The return value is a type object and generally the same object as returned by
object.__class__
.The
isinstance()
built-in function is recommended for testing the type of an object, because it takes subclasses into account.
Let's see. Is int
a type object?
>>> type(int)
<class 'type'>
A class object means an instance of what? In Python 3, classes are always new-style classes so they constitute a type
of themselves:
>>> class X:
... pass
...
>>> y = X()
>>> type(y)
<class '__main__.X'>
But with Python 2.x's old-style classes:
>>> class X:
... pass
...
>>> y = X()
>>> type(y)
<type 'instance'>
>>> type(X)
<type 'classobj'>
Here, the class has types.ClassType
and the instance has types.InstanceType
(neither of which exists in Python 3.x)
A function object is a types.FunctionType
:
>>> def foo():
... return 42
>>> type(foo) is types.FunctionType
True
>>> type(lambda x: x + 2) is types.FunctionType
True
A module object is a types.ModuleType
:
>>> import sys
>>> type(sys) is types.ModuleType
True
And yes, there are int
objects. Consider type(12)
.
Upvotes: 3
Reputation: 103
In Python you can represent types as objects themselves. So yes, they're type objects. For every type there is an object representing that type, like int
. It's an instance of the type itself.
It's like .getClass()
and Class<?>
objects in Java: "some string".getClass() == String.class
. Java doesn't treat functions as first class values so there's types like Callable
, Consumer<T>
, etc., but in Python there's a specific type for functions, and with that a specific object representing that type.
Upvotes: 1