What is the object in python

Reprinted from the product is slightly Library  http://www.pinlue.com/article/2020/04/0621/0410123168102.html

From the beginning of the design is an object-oriented language, it has an important concept that everything is an object.

Although Java is an object-oriented programming language, but no blood pure Python. For example, Java is one of the eight basic data type int, the persistence of the time, you need packaged into Integer class object. But in python, everything is an object. Numbers, strings, tuples, lists, dictionaries, functions, methods, classes, modules, etc. is an object, comprising your code.

Concept of the object

What is an object? Different programming languages ​​define "object" in different ways. In some, it means that all objects must have attributes and methods; other languages, it means that all objects can subclass.

In Python, is loosely defined, some objects have neither attributes nor methods, and that not all objects can be subclassed. Everything but the Python object in the sense can be interpreted as: Python in everything can be assigned to variables or passed to the function as a parameter.

All Python objects have three characteristics:

Identity: Each object has a unique identity of its own, the identity of any object can use the built-in function id () to get, you can simply think that this value is the memory address of the object.

1

2

3

>>> a = 1

>>> id(a)

>>> # 26188904 identity is represented by a string of similar such digital

Type: The type of an object will determine what type of object values ​​can be saved, what properties and methods, which operations can be carried out, how to follow the rules. You can use the object type to view the built-in function type ().

1

2

3

4

>>> type(a)

<type 'int'>

>>> type(type)

<Type 'type'> # Everything objects, object type is a special type

The data object represented by: Value

1

2

>>> a

1

"Identity", "type" and "value" is assigned at the time of all objects created. If the object supports the update operation, the value of which is variable, or (immutable number, string, a tuple etc.) is read-only. As long as there are objects, these three characteristics have been present.

Object attributes: Most Python object has attributes, values, or a method using a dot notation access attributes (.). The most common is a function of the properties and methods, some Python data objects have attributes, such as: classes, modules, files, etc.

More Python-related technical articles, please visit Python tutorial section to learn!

 

 

Published 60 original articles · won praise 58 · Views 140,000 +

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/105361352