Python has been an object-oriented language from the beginning. It has an important concept that everything is an object.
Although Java is also an object-oriented programming language, its pedigree is not as pure as Python. For example, int, one of Java's eight basic data types, needs to be packaged into an Integer class object when persisted. But in python, everything is an object. Numbers, strings, tuples, lists, dictionaries, functions, methods, classes, modules, etc. are all objects, including your code. (Recommended learning: Python video tutorial)
The concept of objects
What exactly is an object? Different programming languages define "objects" in different ways. In some languages, it means that all objects must have properties and methods; in other languages, it means that all objects can be subclassed.
In Python, the definition is loose, some objects have neither properties nor methods, and not all objects can be subclassed. But everything in Python is an object, which can be explained perceptually as: everything in Python can be assigned to a variable or passed as a parameter to a function.
All objects in Python have three characteristics:
Identity: Each object has a unique identity to identify itself. The identity of any object can be determined using the built-in Function id() to get it, you can simply think of this value as the memory address of the object.
>>> a = 1 >>> id(a) >>> 26188904 # 身份由这样一串类似的数字表示
Type: The type of an object determines what type of values the object can save, what properties and methods it has, what operations it can perform, and what rules it follows. You can use the built-in function type() to check the type of an object.
>>> type(a) <type 'int'> >>> type(type) <type 'type'> #万物皆对象,type 也是一种特殊的对象 type
Value: The data represented by the object
>>> a 1
"Identity", "Type" and "Value" are assigned when all objects are created. If the object supports update operations, its value is mutable, otherwise it is read-only (numbers, strings, tuples, etc. are all immutable). These three properties persist as long as the object exists.
Object attributes: Most Python objects have attributes, values, or methods. Use the period (.) notation to access attributes. The most common attributes are functions and methods. Some Python objects also have data attributes, such as classes, modules, files, etc.
For more Python related technical articles, please visit the Python Tutorial column to learn !
The above is the detailed content of What is an object in python. For more information, please follow other related articles on the PHP Chinese website!