How to understand that everything in Python is an object?

introduction

I learned multiple programming languages ​​and became increasingly confused. We know that in C language, each variable needs to declare the variable type, and the parameter type must be consistent when calling the function. Python variables do not need to declare types, and are not even allowed to be declared in advance. Python greatly lowers the threshold for program development (at the expense of performance). This article focuses on solving 2 problems:

  • How to implement Python without declaring data types
  • What does the python language itself embodydesign patterns?

This article uses the first question to explore the underlying implementation of python language design, and the second question to explore the high-level design pattern ideas in python language design.

1.Python

Python is written in C language and also contains many standard libraries written in C language. One of the main reasons why Python uses the C language is because of the efficiency and portability of the C language. Python's open source code is hosted on GitHub, and its warehouse address is: https://github.com/python/cpython/

1. What is the difference between Python language and C language?

  • Syntax: Python uses an indentation style and uses colons to represent code blocks, while C language uses braces to represent code blocks.

  • Type: Python isa dynamically typed language, various variables can be dynamically bound at runtime Different types; while C language isstatically typed language, the variable type is determined during compilation.

  • Automatic memory management: Python automatically manages memory, and programmers do not need to manually manage memory; while C language requires manual memory management, including memory allocation and release.

  • Compilation and interpretation: C language needs to be compiled into binary machine code before execution; while Python is an interpreted language and is directly interpreted The processor interprets and executes.

  • Application scenarios: C language is suitable for writing system-level applications such as operating systems and compilers; while Python is suitable for data analysis, web development, artificial intelligence and other fields.

In general, Python is a simpleeasy-to-learn, high-efficiency, and highly readable languagesuitable for that can Directly operates hardware and memory, suitable for writing high-performance, low-level system-level programs. low-level language; and C language is aRapidly develop prototypes and process data

2. How to implement Python without declaring types?

Python is a dynamically typed language. Variables do not need to declare data types before using them. This is mainly achieved in the following ways:

  1. Type inference: Python infers its type at runtime by analyzing information such as code syntax and the initial value of variables. This is in line with Python's "natural" philosophy and also makes it easier for developers to understand and maintain the code.

  2. Dynamically apply for memory: When Python applies for memory, it dynamically allocates memory based on the variable type. If the value changes type during assignment, Python will automatically release the original memory and re-apply for the required memory.

  3. Reference counting: Python uses reference counting to track the life cycle of variables. Every time an object is referenced, its reference count is incremented by 1. Every time an object loses a reference, its reference count is decremented by 1. When the reference count reaches 0, Python will release the memory space occupied by the object.

  4. Object model:Everything in Python is an object, and the execution of code is the interaction and operation between objects. Therefore, a variable in Python refers to an object rather than a memory address, which can avoid pointer errors, memory leaks and other problems that may occur in other languages.

To sum up, Python achieves the feature that variables do not need to declare types through type inference, dynamic memory allocation, reference counting, and object models.

3. How to understand that everything in Python is an object?

In Python, "Everything is an object" is an important concept. This means that every element in Python—including numbers, strings, data structures, functions, modules, and more—is treated as an object. "Object" is an abstract concept that contains data and behavior. Data can be values ​​of various types, and behaviors are actions or operations that an object can perform. In Python, every object has a type, which defines what operations the object can perform and what methods it supports.

In Python, all classes inherit from a base class namedobject. This means that if you define a class in Python without specifying any of its superclasses, then it will automatically become a subclass of object. The object class contains some special methods (also called "magic methods"), which are very important for the operation of objects. For example, the __init__() method is used to initialize the object, the __str__() method is used to convert the object to a string, and the __eq__() method is used for comparison Whether two objects are equal and so on.

Example 1 Execute the following code: if (i==0)

In C language, the above code will be compiled into a conditional jump instruction (such as JZ or JE), which will check the variable < Whether the value of /span>i is equal to 0, if equal, jump to the if statement for execution, otherwise skip the if statement.

In Python, the above code will be parsed by the interpreter as a Boolean comparison operation i==0, and then based on the result of the comparison, it is decided whether to execute the code block inside the if statement. The Python interpreter will compare objects between i and 0, that is, execute i.__eq__(0) and decide whether to execute the if statement based on the comparison result. code block.

Example 2 String concatenation

The underlying implementation in C language is relatively primitive, and strings exist in the form of character arrays. In C language, the strcat() method is used to merge character arrays. The merged string is directly stored in the original character array, and a new memory space will not be opened separately. In C language, you need to implement a string merging function yourself, and you cannot directly use operator overloading to implement string addition.

In the python language, string splicing can be directly performed throughstr = str1+str2. The operator overloading is used, that is, when str1 + str2 is executed, Python will actually execute str1.__add__(str2) operation, instead of simply splicing two strings together, the bottom layer encapsulates the implementation logic of adding new memory, copying old strings, splicing new strings, deleting old strings, etc. The code implementation is relatively simple, but since strings are immutable objects, a new object will be created every time they are added, which will cause additional memory overhead.

The following is a summary of common magic methods or special methods and their functions in the object class in Python:

magic method Function
__class__ Call type() function to access the class attribute of the object
__delattr__(self, name) Defines the method to be called when trying to delete a property of an object
__dir__(self) Defines how to respond to object-independent dir() built-in function calls
__doc__ Display the object's docstring
__eq__(self, other) Supports the equality operator (==)
__format__(self, format) Define the method called when formatting the object, using the string's format() method
__ge__(self, other) Supports greater than or equal to operator (>=)
__getattribute__(self, name) Defines the method to be called when the user attempts to access a property of an object
__gt__(self, other) Supports greater than operator (>)
__hash__(self) Returns the hash value (must be an integer)
__init__(self[, args...]) Constructor, used to perform initialization operations when creating an object
__init_subclass__() Called when a subclass of this class (not an instance) is created
__le__(self, other) Supports less than or equal to operator (<=)
__lt__(self, other) Supports less than operator (<)
__ne__(self, other) Supports the inequality operator (!=)
__new__(cls[, args...]) Methods used to create an object and return that object
__reduce__(self) is used to provide support for the pickle module
__reduce_ex__(self, protocol) Same as above, but supports more protocols
__repr__(self) String representation used to serve debugging
__setattr__(self, name, value) Defines the method to be called when the user attempts to set a property of the object
__sizeof__(self) Returns the number of memory bytes occupied by the object
__str__(self) User gets the string representation of the object
__subclasshook__(cls, subclass) Called when a subclass inherits, used to check whether the subclass is legal

It should be noted that not all of these methods need to be overloaded and implemented in our own classes, but can be selectively implemented according to actual needs. At the same time, there are many other built-in functions and built-in types in Python, all of which have their own special uses and common magic methods.

4.int type

We take the integer typeint as an example to continue explaining the underlying mechanism of Python. int inherits from the object class, and Some new built-in functions have been added for operations on integer types:

  • __add__(self, other): used to implement integer addition, equivalent to self + other;
  • __sub__(self, other): used to implement integer subtraction, equivalent to self - other;
  • __mul__(self, other): used to implement integer multiplication, equivalent to self * other;
  • __truediv__(self, other): used to implement integer division, equivalent to self / other;
  • __floordiv__(self, other): used to implement integer division, equivalent to self // other;
  • __mod__(self, other): used to implement integer modulo operation, equivalent to self % other;
  • __pow__(self, other[, modulo]): used to implement integer exponentiation operation, equivalent to self ** other;
  • __lshift__(self, other): used to implement bitwise left shift operation, equivalent to self << other;
  • __rshift__(self, other): used to implement bitwise right shift operation, equivalent to self >> other;
  • __and__(self, other): used to implement bitwise AND operation, equivalent to self & other;
  • __or__(self, other): used to implement bitwise OR operation, equivalent to self | other;
  • __xor__(self, other): used to implement bitwise XOR operation, equivalent to self ^ other;
  • __invert__(self): used to implement bitwise inversion operation, equivalent to ~self;
  • __index__(self): Convert the integer into index form to support some built-in functions and methods, such as list, tuple, dict Wait.

These built-in functions enable integer objects to support different calculations and operations, allowing us to flexibly process integer data.

5. Metaclass

Python metaclass (metaclass) is actually a class used to create classes. It can control the creation behavior and attributes of classes to achieve higher-level object-oriented programming. It can be understood as the ''parent class' of the class objectobject. In Python, metaclass is a very important concept. You can control the creation and behavior of classes through the following attributes:

method name describe
__new__(cls, name, bases, attrs, **kwargs) Used to create and return a new class object
__init__(self, name, bases, attrs, **kwargs) Used to initialize class objects and modify the properties and methods of class objects
__call__(self, *args, **kwargs) is used to customize the class object instantiation process. Including instance creation and initialization process. It can implement many advanced object-oriented programming functions, such assingle case mode, object pool, etc.
__getattribute__(self, name) Used to obtain the attributes of an object. Metaclasses can overload this method to implement attribute access control and management.
__setattr__(self, name, value) Used to set the properties of an object. Metaclasses can overload this method to achieve more fine-grained property setting control and management.
__getattr__(self, name) A function that needs to be called when obtaining non-existent attributes of an object. Metaclasses can overload this method to implement default values ​​of attributes, lazy calculation, etc.

6.Type() function creates a class

In the internal implementation of Python,object class is defined in C language, and its data structure is actually a C structure (struct), which stores the type information of the object and some Special flag. This C structure is called the Python object header and is defined as follows:

typedef struct _object {
    
    
    _PyObject_HEAD_EXTRA
    Py_ssize_t ob_refcnt;
    struct _typeobject *ob_type;
} PyObject;

Among them, _PyObject_HEAD_EXTRA represents some additional header information, ob_refcnt represents the reference count of the object, ob_type points to the object The type information it belongs to, that is, the class object of the object.

In Python, class objects are created through the type() function for all objects, including the object class itself. When we define a new class, we actually dynamically create a new class object through the type() function and bind it to the name of the class, thereby creating a new class . For example:

class MyClass:
    pass

When the above code is executed, the Python interpreter will automatically call the type() function to create a new class object named MyClass and then bind it Assigned to a variable name MyClass, thus making MyClass a usable class. You can use the type() function to verify the class object to which the object belongs, for example:

obj = MyClass()
print(type(obj))  # <class '__main__.MyClass'>
print(type(MyClass))  # <class 'type'>
print(type(object))  # <class 'type'>

As can be seen from the output, obj is an instance of the MyClass class object, and the MyClass class itself is Created by the type class, and the type class itself is created by the object class, which also explains The object class is the most basic class in Python.

Guess you like

Origin blog.csdn.net/weixin_41099712/article/details/129974326