Python BASICS (2): object, 2015 python

Source: Internet
Author: User

Python BASICS (2): object, 2015 python

Python uses an object model to store data. Constructing a value of any type is an object.
Python objects have three features: identity, type, and value.

Identity is the unique identity of each object. Any object can use the built-in function id () to obtain its identity. For example:

>>> s = 1>>> id(s)38249176

Identity can be considered as the address of the object.

The data type is the method in which data is stored, the operations that can be performed, and the rules that can be followed. You can use the built-in function type.

The value is the data item of the object.

The preceding three features are assigned values when an object is created. All features except values are read-only. The object type can be changed, but this is not recommended for beginners.

Python uses the dot mark method to access attributes, including object names. The most common attributes are functions and methods, but some also have data attributes. Objects containing data attributes include (not limited to) classes, class instances, modules, plural numbers, and files.

Python standard types include: Numbers, integer, Boolean, long integer, floating point, plural, String, list, tuples, and dictionary. There are also the following built-in types: type (yes, the type itself is a type), Null object, file, set, function/method, module, class.

Type is also an object, because a series of inherent behaviors and features of the object need to be defined in advance, we use the type object to save the information.
You can use the built-in function type () to obtain the type information of a specific object.

>>> type(7)<type 'int'>

<Type 'int'> is output here. In fact, it is not simply a string like an integer, but a type object, this object will output a string to tell you that it is an object. We can test it like this.

>>> type(type(7))

If this is only a string, it will display <type 'str'> if it is an object type, it will display <type 'type'>

The result is

>>> type(type(7))<type 'type'>

 

None, Null Object

This is a special type. It has only one value of None, which is similar to the void of C. There is no built-in method and no useful attribute. The Boolean value is always False.

 

There are also some internal types implemented using classes, which will not be described here.

Standard Operators
Object Value Comparison
Returns True or False if the objects of the same type are equal. Operators include ==, <=, and >=. Multiple comparisons can be performed in the same row, which is different from that in C. Sequential comparison from left to right

>>> 3 < 4 < 5True>>> 3 < 5 < 4False

This form is written in most languages.

>>> 3 < 4 and 4 < 5True>>> 3 < 5 and 5 < 4False

At the same time, you can use the following statement, which is generally not written in mathematics.

>>> 3 < 5 > 4True

 

Object Identity comparison
This is a supplement to value comparison, and Python also supports object comparison.
The following statement is used:

a is b

It is used to compare whether the variables a and B point to the same object. It is different from a = B.

A is B is equivalent
Id (a) = id (B)
The following code is available:

>>> foo1 = 2.0>>> foo2 = 1.0 + 1.0>>> foo1 == foo2True>>> foo1 is foo2False>>> foo1 = 2.0>>> foo2 = 2.0>>> foo1 is foo2False>>> foo2 = foo1>>> foo1 is foo2True>>> foo3 = 2>>> foo4 = 1 + 1>>> foo3 is foo4True

Here we can see that the values of foo1 and foo2 are equal, but their identities are not equal. That is to say, it is not from the same reference. It means that two objects are created and referenced respectively when they are stored internally.

Then we can see that when I assign 2.0 values to both foo1 and foo2, they are not equal. Two objects are created. Only by assigning foo1 directly to foo2 can they point to the same object.
From then on, we can understand the internal value Assignment Method in Python. Each value is created for a new object and then referenced to the variable.
However, if it is an integer, the result is different. Description in the book:
Integer objects and string objects are immutable objects, so Python caches them very efficiently. This causes us to think that Python should create a new object without the illusion of creating a new object.
That is to say, integer and floating point types are different because Python caches them efficiently. However, this cache has a range and must be used with caution.


 

Standard built-in functions
Function Function
Cmp (obj1, obj2) Compare obj1 and obj2, and return the integer I based on the comparison result:
I <0 if obj1 <obj2
I> 0 if obj1> obj2
I = 0 if obj1 = obj2
Repr (obj) or 'obj' Returns the string representation of an object.
Str (obj) The returned object is suitable for string representation with good readability.
Type (obj) Get an object type and return the corresponding type object

 

Repr () returns an object's "official" string representation. That is to say, in most cases, this object can be re-obtained through evaluate (using the eval () built-in function), but str () it is different. Str () is used to generate a readable string representation of an object. Its returned results cannot be used for eval () evaluation, but are suitable for print statement output. That is to say, repr () outputs are friendly to Python, while str () outputs are friendly to humans. Even so, in many cases, the output of the three items is still the same. The so-called "Python-friendly" and "user-friendly" will be mentioned in the value section.

Type factory Functions
Python2.2 unifies types and classes, and all types are classes. The original built-in conversion functions such as type () and int () have become factory functions, that is, although they look like functions, but it is actually a class. An instance of the type is generated during the call.
For the class part, because I have not learned much about the C language, I will take notes after learning the class part.

In the storage model,
There are scalar/atomic types that store one or more values, such as values and strings.
There are container types, such as lists, tuples, and dictionaries.
Because Python does not have the char type, the string is a self-contained text type, not the same as the array in C.

Update model,
After an object is created, the model can be updated, but the immutable object cannot be changed. Lists and dictionaries are variable models, while numbers, strings, and tuples are immutable models.
There is a misunderstanding here. Values and string variables can be changed, but objects cannot be updated. Each transformation creates an object again.
That is to say, the value of id () changes after each transformation. The dictionary and list change the value itself, and the id () remains unchanged.

Access Model,
This category is the primary type for distinguishing data types. Data is classified by access to the stored data. There are three access methods: direct access, sequence, and ing.
Direct access to numbers
Sequential access string, list, And tuples
Ing access dictionary
Direct access is not described in detail. sequential access refers to sequential access of elements starting from 0. One or more elements can be accessed at a time.
The ing type is similar to the index attribute of the sequence type, but it does not apply to the address offset value. The elements are stored unordered and accessed by a unique key. Yeskey-value set

One reason for classifying data types is that, for advanced data provided by Python, we need to distinguish the original type from the powerful extended type, so we need to understand the classification. The second is to help us find out the characteristics of each type.
The reason for classification from different aspects is that the relationship between these data structures is complex. We need to clarify the context multiple times to give us a deeper understanding of the types.

Types not available in Python
Char or byte. Python does not have an 8-digit integer. It can be replaced by a string of 1.
Pointer. Python helps programmers manage memory, so there is no need to access pointers. The value obtained through id () is very close to the address, but cannot be operated, so the pointer is useless.
There is no difference between short and full length, so that programmers can focus more on what they should do.
There is no single-and double-precision floating point type. Make things easier.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.