- Python Learning Note _python Object
- Python Object
- Standard type
- Other built-in types
- Type Object and type Object
- Null object of Python None
- Standard type operator
- Comparison of object values
- Object Identity Comparison
- Boolean type
- Built-in functions of standard types
- Typeobj
- Cmpobj1 Obj2
- Strobj Reprobj
- Typeobj Isinstanceobj
- Classification of standard types
- Storage model
- Update model
- Access model
- Unsupported types
Python Learning Note _python Object
First of all to understand a popular meaning, what is 对象
? In fact, whatever language the object is in, for example C++
, Python
it refers to a type of memory, the object contains the corresponding data
Python Object
All Python objects have three properties: 身份
, 类型
值
- 身份
: Each object has a unique identity ID to identify itself, just as the object is stored in memory and is unique and not shared. The identity identifier of any object can be used in the built-in function id()
to get
x=1print id(x)#运行结果:163449008
类型
: Because Python is a dynamic type, it is within an object that you should save the object's type to determine what kind of action to take on that object. We can type()
get the type of the object through the built-in function.
1‘Hello World‘print type(number1)print#运行结果#<type ‘int‘>#<type ‘str‘>
值
: object represents the data item, we store the data in memory, if we store it in memory 1000001
, if it is str
type, then it is interpreted as ‘A‘
, if it is, it is interpreted as65
Object Properties : Some objects have properties, values, or associated executable code, and the most common properties are functions and methods
Standard type
- Digital
Integer
Plastic
Boolean
Boolean type
Long integer
Long Plastic
Floating point real number
Floating point Type
Complex number
Plural type
String
String
List
List
Tuple
Meta-group
Dictionarry
Dictionary
Other built-in types
- Type
- Null object (None)
- File
- Set/fixed Set
- Functions/Methods
- Module
- Class
Type Object and type Object
As mentioned before, objects are judged by their type to perform those operations, so the type is the best place to store these operations, so it is more appropriate to use an object to describe a type than a string.
type()
The built-in function returns a type object, except that the object is displayed as a string
print type(10)print type(type(10))#运行结果:#<type ‘int‘>#<type ‘type‘>
The type of all types of objects is type
, it is also the default meta-class for all types of roots and all Python standard classes.
Python's null object--none
None
There is only one value, it does not support any operations, and there is no built-in method.
Each object has a natural True
or a False
value.
The Boolean values for the following objects are False
:
- None
- False
- The number of all values is 0
- 0
- 0.0
- 0L
- 0.0+0.0j
- “”
- []
- ()
- {}
In addition to the above values, the other values areTrue
Comparison of standard type Operator object values
The comparison of object values returns a bool object directly, and the comparison methods for objects of various types are also different. , a numeric type is compared by the size and symbol of a numeric value, and the string follows a sequence of characters.
Object Identity Comparison
In Python, it is common to first create an object in memory and then assign the object's reference to the variable, manipulating the object by reference.
3.14# 创建一个3.14浮点型对象,并将引用赋值给变量x
In many cases, a reference to an object is passed to a variable, in order to detect whether two variables point to the same variable at the same time, we can use the built-in function id()
to compare two variables to the equality of the object id
, or is
is not
to compare whether two variables point to the same object
Give me a chestnut:
3.14y = xprint id(x) == id(y)printis# 和 id(x) == id(y) 一样printisnot# 和 id(x) != id(y) 一样
Operation Result:
True
True
False
Give me a chestnut:
3.141.02.14printis# 虽然数值相同,但是并不指向同一个对象
Operation Result:
False
Boolean type
When multiple Boolean conditions require a phone to be judged, Python
we provide the following statements
operator |
function |
and |
Logic and |
Or |
Logical OR |
Not |
Logical Non- |
Built-in functions of standard types
In addition to the operators, some built- Python
in functions are provided for these basic information:
function |
function |
CMP (OBJ1, OBJ2) |
Comparison of two objects for equality |
Repr (obj) |
Returns a string representation of an object |
STR (obj) |
Returns a string representation of an object for good readability |
Type (obj) |
Returns the type of the object, which is a type object |
Type (OBJ)
type()
The built-in function is used to return the type of an object.
print type(1)print type(‘Hello World‘)
Operation Result:
CMP (OBJ1, OBJ2)
cmp(obj1 , obj2)
is used to compare two objects.
return Results |
comparison Scenario |
Positive integers |
Obj1 > Obj2 |
0 |
Obj1 = Obj2 |
Negative integer |
Obj1 < Obj2 |
The value returned above can be interpreted as the obj1 - obj2
returned result
Give me a chestnut:
12print cmp(x, y)# 运行结果: -1
‘xyz‘‘abc‘print cmp(str1, str2)# 运行结果: 1
STR (obj) & repr (obj)
The two built-in functions above str(obj)
and repr(obj)
all are strings that return an object, but these two built-in functions are different:
str(obj)
: is used to return a well-readable string that is intended for a user who is a programmer
repr(obj)
: is used to return a Python虚拟机
well-understood string, the user-oriented is Python虚拟机
that the repr(obj)
returned string can be eval(str)
evaluated by re-evaluation! That is, the formula is set upobj == eval(repe(obj))
In general, it is repr(obj)
right Python比较友好
, str(obj)
is friendly to the user, but in many cases str
and repr
the output is the same
print str(1)print repr(1)#运行结果:#1#1
Type (obj) & isinstance (obj)
type(obj)
You can return Python
the type of any object, not just the standard type
Give me a chestnut:
For standard types
print type(‘‘)#运行结果: <type ‘str‘>print type({})#运行结果: <type ‘dict‘>
For custom types
class Foo: passfoo = Foo()class Bar(object): passbar = Bar()print type(Foo)#运行结果 : <type ‘classobj‘>print type(foo)#运行结果 : <type ‘instance‘>print type(Bar)#运行结果 : <type ‘type‘>print type(bar)#运行结果 : <type ‘__main__.Bar‘>
If we need to determine the type of an object now, we can use the following statement to judge:
#方式1if type(num) == type(0):...#方式2import typesif type(num) == types.IntType:...#方式3ifis# 因为在运行时只有一个对象来表示整形类型#方式4fromimport IntTypeifis IntType:...#方式5if isinstance(num , int):...
Classification of standard types
There are three different models that can help us categorize the basic types:
- Storage model
- Update model
- Access model
Storage model
In the storage model we can save more objects according to the type of object, divided into two categories:
- Scalar storage Type: Only the type of a single Literal object can be saved
- Container storage type: A type that can hold multiple objects
category |
python type |
Scalar storage Type |
Numeric (all numeric types), string (all literal, because Python has no character type) |
Container type |
list, tuple, dictionary |
Update model
In the python
inside type is divided into can change is not to change the two types, the object that runs the change allows their values to change, and non-changeable objects will not allow their values to change
category |
python type |
Variable type |
List, dictionary |
Immutable types |
Numbers, strings, tuples |
What the? are numbers and strings not to be changed? So what's the change?
‘Hello World‘‘Hello Moto‘
Yes, the value of the above variable does change, but the variable does not represent the object, the first assignment is to assign a reference to the value ‘Hello World‘
str1
, and the second assignment statement is to assign a reference to the value of ‘Hello Moto‘
str1
two string objects ‘Hello World‘
and ‘Hello Moto‘
the values are not changed!
Access model
There are three ways to access the model: 直接存取
, 顺序
and映射
- Direct Access : Provides direct access to non-container objects
- sequence Access : Elements within a container can be accessed from a 0-based index sequence
- Map Access : Access by a unique key
category |
python type |
Direct access |
Digital |
Sequential access |
string, tuple, list |
Map Access |
Dictionary |
Unsupported types
Python
There are some common but unsupported types:
- Char and Byte
- Pointer
- int, short, long
- Single-precision floating-point number
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python Learning Note _python Object