>>> type(type)
<type 'type'>
I expect the type of type to be a function since it's used to return the type of the argument. Does this mean a type can accept arguments? Is type something unusual/special?
type is a metaclass: a class whose instances are also classes. The type of any other builtin class will also be type - eg:
>>> type(object)
<class 'type'>
>>> type(list)
<class 'type'>
Indeed, every (new-style) class will also be an instance of type, although if it is defined with a custom metaclass, type(my_class) will be that metaclass. But since every metaclass is required to inherit from type, you will have, for any class:
>>> isinstance(my_class, type)
True
Classes are objects. All objects are instances of a class. So since a class is an object, it is an instance of some class. The class that a class is an instance of is named type. It is the base metaclass.
Originally type() was just a function that returned an object's class. In Python 2.2, user-defined classes and built-in types were unified, and type became a class. For backward compatibility, it still behaves like the old type() function when called with one argument.
type is Python's class ur-type. When called with a single argument it returns the type of the argument. When called with 3 arguments it returns a class whose characteristics are determined by the arguments.
As can be clearly seen in the documentations -
class type(object)
class type(name, bases, dict)
With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing the type of an object.
type is a class, not a function.
Related
I declare a variable and assign it an integer value, its type is <class 'int>. Shouldn't that be an object?
If I define a class and instantiate its object, the type of the object is again class.
If I assign a variable to object, the variable is <class 'object> and its type is <class 'type'>
(refer the corresponding shell entries)
To be precise, I want to understand what it means when (in tutorials) it is said that everything in Python is an object, when I can see the that type always says it's a class?
Can anyone please explain this discrepancy?
In Python shell:
1.
>>> a = 1
>>> type(a)
>>> <class 'int'>
>>> class Something:
... pass
...
>
>>> s = Something()
>>> type(s)
>>> <class '__main__.Something'>
>>> t = object
>>> t
>>> <class 'object'>
>
>>> type(t)
>>> <class 'type'>
All values in Python are objects, in the sense that each value has a type associated with it. (Variables themselves to not have types; type(a) simply reports the inherent type of whatever value as assigned to the name a.)
Further, classes themselves are first-class values with their own type. Just as the type of 3 is int, the type of int is type. Even type is a value, with the somewhat surprising feature that the type of type is type.
object is the root of the class hierarchy, so all classes have object as an ancestor. (Just as type has type type, object is the parent of object, which is probably less surprising if you are used to any class being a trivial subclass of itself.) All classes, including object, are instances of type type.
With the definition (not declaration) a = 1, you assign the value 1 (with type int) to the name a.
With s = Something(), you create a value of type Something and assign it to the name s.
With t = object, you assign the value object (with the type type) to the name t.
Hmm..., this is not only a vocabulary question. In Python 3 the object class is a special class whose any other class (including int) derives. Everything is an object could more verbosely be said everything is an instance of a class that is a subclass of object.
The last one says that most classes including object are direct instances of another very special class which it type. In fact in Python all classes are instances of their metaclass, and except for special requirement the standard metaclass is type and metaclasses are expected to be subclasses of type
If we want to go one step further, we realize that as type being a class, is a subclass of object: type.__mro__ gives (<class 'type'>, <class 'object'>). On a strict logical and mathematical point of view, it can be seen as rather weird (the set of all sets cannot exists in set theory). But as the basic types and functions of Python are built-in they are not required to be defined in Python language and we do not fall in a definition loop.
I'm currently learning python OOP model and have been told
type itself derives from object, and object derives from type
I understand that object is the default superclass of every class in python 3.x, and type class is used to created classes (i.e. class object). object and type together (somehow) forms the bases of python OOP, but I am still confused as ever about the statement above.
Could someone provide a detailed explanation on the relationship between object and type, and the roles they play in python OOP. Thanks
We need to distinguish subclassing from instantiation. The exact rules can vary with the language, but in Python 3.x--
All classes are subclasses of object (well, except for object itself). object is the root of the class hierarchy.
The type class is a subclass of object.
All objects are instances of a class.
Classes are themselves objects. (This is not true in every language.)
Class objects, being objects, are instances of a class--class objects are instances of the class type (the default metaclass).
Yes, type is a class and an object, and it is an instance of type. type has class type.
Yes, object is a class and an object, and it is an instance of type. object has class type.
You can see the class of an object by using .__class__ or type() on it.
You can see the superclasses of a class by using .__mro__ (method resolution order) on it.
>>> type(object)
<class 'type'>
>>> type(type)
<class 'type'>
>>> object.__mro__
(<class 'object'>,)
>>> type.__mro__
(<class 'type'>, <class 'object'>)
In python 3 everything is a object. I drew a diagram about relation of classes. Is this diagram correct?
the hard part is about type and object classes. how are they in relation ?
type is a object ? or object is a type ?
>>> x=type
>>> type(x)
<class 'type'>
>>> x=object
>>> type(x)
<class 'type'>
As far as I know, the class relations are kind of like this in Python 3:
Every class is a subclass of object
Every class is an instance of type
Every class is created by the type class or an other metaclass, which derives from type. Because of this every class is an instance of type (including type!) Every class will return True for isinstance(cls, type).
In Python 3, every class is also a subclass from object. Every class or instance will return True for isinstance(cls_or_instance, object)
A special case is metaclasses. A metaclass derives from type, so every metaclass will return True for issubclass(metaclass, type) and isinstance(metaclass, type)
The type object is itself an object. Note though that the inheritance model of python is not the same as in other OO-languges, much of it depends on duck typing rather than inheritance.
Note that type(x) returns the type of the object, that type(object) returns <class 'type'> means nothing more than the type of object (which is the type all objects has) is a type (the type all types are), type itself is a type so the type of it is again type.
I've little bit test to fully understand metaclass in python.
class Test(object):
pass
print Test.__class__
print Test.__class__.__class__
print Test.__class__.__class__.__class__
All of result is same type. but each of their address is not same
I can't really understand why metaclass has a metaclass recursively.
Explain me please?
Actually, addresses are the same:
>>> id(Test.__class__)
6384576
>>> id(Test.__class__.__class__)
6384576
>>> id(Test.__class__.__class__.__class__)
6384576
Everything is an object in Python, and each object must have a class (it should belong to some type). You can access that class/type reference by __class__ attribute, e.g.:
>>> (1).__class__
<type 'int'>
Everything includes classes itself, which are of class/type called type:
>>> (1).__class__.__class__
<type 'type'>
In the same time type 'type'> is also an object and should reference to some class/type. But since this is kind of special object, its __class__ attribute refers to itself:
>>> (1).__class__.__class__.__class__ is (1).__class__.__class__
True
When you do Test.__class__, you get back the type of Test, which is type (because Test is a class identifier).
type itself is again a class identifier, so you can call __class__ (which is inherited from object) on it and you get back that its type is, again, type because it is a class identifier.
Because you will always get back type which is a class itself, you can do this infinitely many times and will always get back that the current object's type is type.
All classes are classes which means they are derived from a class called class...
All the python's class object is build by the built-in function type(). You could also try this.
>>> T.__class__ == type
True
>>> type(type)
<type 'type'>
The T.class is equal to the build-in function type which is also an object implemented the call function. It's a attribute will be interpret as class(T). As your T class have no base class so type() is used which will return the type object.
You could check the python doc about customizing class creation to get detail about class creation.
To determining the appropriate metaclass
if no bases and no explicit metaclass are given, then type() is used
if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass
if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used
Here's a piece of code that I cannot understand:
class COWMeta(type):
pass
class COWDictMeta(COWMeta):
....
I know how to create a new class in python:
class MyClass(BaseClass):
...
But as the manual states, 'type' is function.
type(...)
Function of __builtin__ module
type(object) -> the object’s type type(name, bases, dict) -> a new type
How can a class inherit from a function? And what does that piece of code mean?
type is the basic object type in python. Like many object types in python, it acts as a constructor for creating new types, but in it's simplest form it'll return the type of existing objects. It then looks like a function. Compare this to int() and list(), for example.
In python, you can create new types, also called metaclasses, allowing you to do all sorts of powerful and interesting tricks in Python. Basing a class definition on type means you are creating a new metaclass.
See What is a metaclass in Python? for an in-depth answer on what metaclasses are.
type is not a function in the same way that, eg:
def foo():
pass
is a function. It is callable like a function (and like many other objects in Python), but it is actually coded as a class. type itself can show you this difference:
>>> type(type)
<class 'type'>
>>> type(foo)
<class 'function'>
The docs call it a 'function' not because of how it is implemented, but because of how it is commonly used. This is broadly similar to, for example, itertools.permutations, which while not explicitly called a function by the docs is implied to be one:
Return successive r length permutations of elements in the iterable.
But itertools.permutations is implemented as a class:
>>> type(itertools.permutations)
<class 'type'>