2015/8/28 Python Basics (2): Objects

Source: Internet
Author: User

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

Identities are unique identities for each object. Any object can be identified using the built-in function ID (). Such as:

>>> s = 1>>> ID (s)38249176

The identity can be considered to be the address of the object.

Type is how the data is saved, how it can be manipulated, and what rules to follow. You can use the built-in function type () to view.

The value is the object's data item.

The above three attributes are assigned when the object is created, except for the values that are read-only. The type of object can be changed, but for beginners it is not recommended.

Python uses dot notation to access properties, attributes include the name of the object, and so on, the most commonly used attributes are functions and methods, but some also have data properties. Objects containing data attributes include (not limited to): Classes, class instances, modules, complex numbers, and files

The standard types of Python are: numbers, integers, booleans, long integers, floating-point, complex numbers, strings, lists, tuples, and dictionaries. There are also the following built-in types: type (yes, type itself is a type), null object, file, collection, function/method, module, class.

A type is also an object, because a series of intrinsic behaviors and attributes of an object need to be defined beforehand, and we use objects of the type to hold this information.
Use the built-in function type () to get the type information for a particular object

>>> type (7)'int'>

This outputs the <type ' int ', which is not simply a string of integers, but a type object that will output a string to tell you that he is an object. We can test it like this.

>>> Type (Type (7))

If this is just a string, it will show <type ' str ' > If it is an object type, it will show <type ' type ' >

The result is

>>> Type (Type (7))'type'>

None,null Object

This is a special type, it has only one value is none, similar to the void of C, there is no built-in method, no useful property, and the Boolean value is always False

There are also some internal types implemented with classes, which are not mentioned here.

Standard type operators
Comparison of object values
Comparison operators to determine whether objects of the same type are equal and return true or false. Operators are = =, <=, >=. Multiple comparisons can be made on the same line, which differs from C. The order of comparisons is left-to-right

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

This form is written in most languages.

 and 4 < 5True and 5 < 4False

At the same time, you can use the following notation, generally do not write in mathematics

>>> 3 < 5 > 4True

Comparison of object identities
This complements the comparison of values, and Python also supports comparisons of the objects themselves.
The following statement is used

 is b

It is used to compare whether a, b two variables are pointing to the same object. and a = = B are not the same.

A is B equivalent to
ID (a) = = ID (b)
Like the code below

>>> foo1 = 2.0>>> Foo2 = 1.0 + 1.0>>> foo1 = = foo2true is foo2false
   
    is 
     foo2false>>> foo2 =
     foo1
     is
     foo2true
     is
     foo4true 
   

You can see that the values of foo1 and Foo2 are equal, but their identities are not equal. That is to say, not from the same reference. Note When you store them internally, you create two objects and then reference them.

Then I can see that when I assign 2.0 to foo1 and Foo2 at the same time, they are not equal. is to create two objects, and only assign foo1 directly to Foo2 to make them point to the same object.
From then on, we can understand the method of assignment within Python, and each value creates a new object and then references it to the variable.
However, if it is an integer, the result is different. The book explains:
Integer objects and string objects are immutable objects, so python caches them efficiently. This causes us to think that Python should create new objects without the illusion of creating new objects.
That is, integers and floating-point types appear differently because Python caches them efficiently. However, this cache is scoped and must be used with care.


Standard type built-in functions
Function Function
CMP (OBJ1, OBJ2) Compare Obj1 and Obj2, and return the integer I as a result of the comparison:
I < 0 if obj1 < obj2
I > 0 if obj1 > obj2
i = = 0 if obj1 = = Obj2
Repr (obj) or ' obj ' Returns a string representation of an object
STR (obj) Returns an object that fits into a good-readability string representation
Type (obj) Gets the type of an object and returns the corresponding type Object

Repr () returns an "official" string representation of an object, meaning that in most cases the object can be re-obtained by an evaluation operation (using the eval () built-in function), but Str () is different. STR () is committed to generating an object's readable string representation, and its return results are usually not available for eval () evaluation, but are well suited for print statement output. This means that the repr () output is friendly to Python, and the output of STR () is more friendly to people. Even so, in many cases the output of the three is still exactly the same. The so-called friendly and friendly to Python will be mentioned in the value section.

Type Factory functions
Python2.2 unifies the types and classes, all types are classes, the original built-in conversion functions such as type (), int () are factory functions, that is, although they look like functions, they are actually classes. Called when an instance of a type is generated.
About the class part, because I learn the C language is not too much involved, wait until the class part of the study to take notes.

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

Update the model,
After an object is created, you can update the value of the update model, and not allow changes to be made to immutable objects. Lists and dictionaries are mutable models, and numbers, strings, and tuples are immutable models.
There is an understanding deviation, the value and the string variable can be swapped, but the object is not updatable, each transformation is re-created object.
This means that the value of the ID () changes after each transformation. While dictionaries and lists change the value itself, the ID () is always the same.

Access the model,
This classification is the primary type that distinguishes data types. Classify them in a way that accesses the data we store. There are three ways to access: Direct access, sequencing, mapping.
Direct access to numbers
Sequential access to strings, lists, tuples
Map Access Dictionaries
Direct access does not repeat, sequential access is the sequence type, that is, the element is indexed sequentially from 0 access, one or more elements can be accessed at one time.
A mapping type is similar to an indexed property of a sequence type, but does not apply an address offset value, which is stored out of order and accessed through a unique key. is a key-value collection

The reason for classifying data types is that the advanced data provided by Python, we want to distinguish between the original type and the powerful extension type, so we need to understand the classification. The second is to help us figure out the characteristics of each type.
The reason for classifying them in different ways is that the relationships between these data structures themselves are complex, and we need to be clear on a number of occasions so that we can understand the types more deeply.

Python does not have a type
char or byte. Python does not have a 8-bit integer. Can be substituted with a string of length 1.
Pointer. Python helps programmers manage memory, so they don't have to access pointers. The value obtained by ID () is close to the address, but cannot be manipulated, so pointers are useless.
There's no difference between short and long, so programmers are more focused on what to do.
There is also no single-double-precision floating-point type. Make things easier.

2015/8/28 Python Basics (2): Objects

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.