preface

I still remember that when I learned Python, I felt confused about everything object in Python, because Python is an object-oriented dynamic language, and in the application of functions and higher-order functions, if I did not have a thorough understanding of everything object and the foundation was not so solid, then for the future advancement of Python, It is a bit of a drag, in view of a friend contacted me to say, for everything is not too solid grasp of the object, so, Muzi’s blog summarizes the knowledge points. If have undeserved place, still hope each big guy corrects.


The paper

To make a long story short, this article focuses on Python everything objects, mainly from the following two aspects:

A first-class citizen of Python

Type, object, and class relationships

Common Python built-in types are not covered in detail in this article

Built-in types: None (only one global type) Numeric types: int, float, complax (plural), bool Iteration type Sequence types: List, bytes, range, tuple, STR, array Mapping type: dict Set type: Set, frozenSet Context management type: with


A first-class citizen of Python

What is a first-class citizen?

  • Can be assigned to a variable
  • Can be added to a collection object
  • Can be passed to a function as an argument
  • Can be used as the return value of the function

For assigning to a variable and adding to a collection object, the code shows:

def func(name='Python') :print(f'the func is {name}')


class Test:
    def __init__(self):
        print(f'the class is {Test.__name__}')

obj_list = []
obj_list.append(func)
obj_list.append(Test)

for item in obj_list:							Add to the collection object
    print(item())
Copy the code

So in the above example, the code runs as follows:

the func is Python								# function func runs and prints the result
None											# func returns None because there is no return
the class is Test								Run class Test and print the result
<__main__.Test object at 0x0000024AB34526A0>	Item () is called to return an object of class Test
Copy the code

So for those that can be passed as arguments, the following code shows:

def func(name='Python') :print(f'the func is {name}')

def decorator_func():
    print(f'the decorator_func is running')
    return func									Return function func

my_test = decorator_func()
my_test('Three Dimensional World of Muzi')						Pass the argument and call func
Copy the code

The results of the above code are as follows:

Decorator_func the decorator_func is running the func is the 3d world of woodCopy the code

Type, object, and class relationships

Type, object, and class are all objects in Python. They are all objects in Python. They are all objects in Python. For ease of understanding, the code is shown:

# example:
a=2019
b="Everything is an object"
print(type(2019))
print(type(int))
print(type(b))
print(type(str))

class Student:
    pass

stu = Student()
print(type(stu))
print(type(Student))
print(int.__bases__)
print(str.__bases__)
print(Student.__bases__)
print(type.__bases__)
print(object.__bases__)
print(type(object))
print(type(type))
Copy the code

The above code runs as follows:

<class 'int'>									# 2019 is an instance created by the int class
<class 'type'>									# int This class is an instance created by the type class
<class 'str'>									# same as above
<class 'type'>								
<class '__main__.Student'>						# stu is an instance created by class Student
<class 'type'>									The # class Student is an instance created by the type class
(<class 'object'>,)								The base class of int is the object class
(<class 'object'>,)								# same as above
(<class 'object'>,)								# same as above
(<class 'object'>,)								The base class of type is also the base class of Object(a)Class Object has no base class
<class 'type'>									# Difficulty: Class object is an instance created by class type
<class 'type'>									# Difficulty: Class type is an instance created by class type itself
Copy the code

For the above code, we can draw the following conclusions:

  1. Class type generates an instance of int, which in turn has an instance of 2019
  2. The object class is the topmost base class
  3. Type is also a class and an object

Must be some friends, see the above code, has a little confused. See the picture below:

Here’s how to interpret the picture above:

  1. Object is the base class for all objects: list, STR, dict, tuple, and object is an instance of Type
  2. The class type is an instance of itself, and type also inherits from the Object class
  3. From conclusions 1 and 2, it follows that everything is an object and everything inherits from the Object class

conclusion

Python Everything objects are great for functional programming, as well as various higher-order uses of Python. If any of you are not sure, take a look at the pictures above. All right, here we go again

I’m sure you will have a better understanding of Python metaclass. I will update a blog about Python metaclass in the future.

Note: why type class can own instance, interested friends can understand the concept of pointer.

It’s late, go to bed, work tomorrow Monday…