osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

An "Object" class?


On 8/27/2019 2:19 PM, Cristian Cocos wrote:
> Thank you! What would be the names of the *class *class,
> and of the *function *class please?

Use type(ob) to get the internal name of the class of an object.
Built-in classes that users may need to call in python code are bound to 
the same name in __builtins__, as if __builtins__ were build with class 
statements. but many are not.

 >>> type(object)
<class 'type'>
 >>> type(type)
<class 'type'>
 >>> type(int)
<class 'type'>
 >>> type(abs)  # built-in function
<class 'builtin_function_or_method'>

Users can usefully call 'type' but not 'builtin_function_or_method'.

 >>> def(f): pass

 >>> type(f)  # User-defined function.
<class 'function'>
 >>> l = lambda: None  # Equivalent lambda expression.
 >>> type(l)
<class 'function'>

Creating a function by calling 'function' instead of using 'def' or 
'lambda' is a super expert endeaver, and the details are CPython 
specific and subject to change in any version.

 >>> type(list.__add__)
<class 'wrapper_descriptor'>
 >>> type(list.append)
<class 'method_descriptor'>
 >>> for k, v in vars(list).items(): print(k, type(v))
# 36 lines.

To see super classes of a class, use type.mro(cls).  The returned list 
always begins with cls and ends with 'object'

 >>> mro = type.mro
 >>> mro(object)
[<class 'object'>]  # As Chris said, baseclass has no superclass.
 >>> mro(type)
[<class 'type'>, <class 'object'>]
 >>> mro(list)
[<class 'list'>, <class 'object'>]
# Nearly all built-in classes return [cls, 'object'].
 >>> mro(bool)
[<class 'bool'>, <class 'int'>, <class 'object'>]
# The only exception I can think of.

-- 
Terry Jan Reedy