Python notes 2 dynamic types of Python objects

Source: Internet
Author: User

Everything in Python is an object, and these objects are part of Python, that is, built-in objects, which are generated together with Python. From a more formal point of view, in P Ython, the data appears as an object one by one whether it is a built-in object provided by the PY T hon, or an object created using Python or an extended language tool such as the C extension library. Although this concept can be determined later, the object is nothing more than a part of memory, containing a collection of values and related operations. Built-in Object object type example constants/create digital 1234, 3.1415, 3+4j, Decimal. Fraction string ' spam ', "Guido '", B ' a fly Xolc ' list [1, [2, ' Three '], 4] Dictionary {' Food ': ' spam ', ' taste ': ' Yum '} tuple (1, ' spam ', 4, ' U ') file Myfile=open (' eggs ', gamma) set set (' abc '), {' A ', ' B ', ' C '} other type type, None, Boolean programming unit type function, module, class (see part fourth, Part V and sixth) with implementation-related type compilation Code heap shuttle Tracking It is equally important that once an object is created, it binds to the operations collection with one by one actions that can be string-related to the character rate and list-related operations. It's like you're going to learn. Python is a dynamic type (it automatically tracks your type rather than asking for a claim code). But it is also a strongly typed language (you can only perform a valid operation on an object that is appropriate for that type). Numbers, strings these are objects in Python, and once an object is created, the operations associated with it (the object's method call) are bound. The dynamic type dynamic type of Python is the source of the flexibility of P YT h o n language. Lack of type declaration if you have a background in learning statically compiled type languages C, C + +, or J Ava, you may be a little puzzled to learn here. Until now, when we were using variables, we didn't declare the existence and type of the variables, but the variables would work. For example, in an interactive session mode or in a program file, when you enter a = 3 o'clock. How does Python know that it represents ding an integer? In this case. How does Python know what A is? in P y th on, the type is automatically determined during the run, not by the code declaration. This means that there is no need to declare variables beforehand. Variable a changes its type as its value is changed. >>> a=3>>> Type (a) &ltclass ' int ' >>>> a= ' cc ' >>> type (a) <class ' str ' >>>> print (a) cc>>> a=10 >>> Print (a) The 10>>> variable creates a variable (that is, the variable name), like a, which is created when the code assigns it the first time. The subsequent assignment will change the value of the variable name that was created. Technically, Python detects variable names before the code runs, and can create variables as initial assignments. A variable type variable never has any type information or constraints associated with it. The concept of a type is present in the object and not in the variable name. The variable is originally generic, it simply refers to a specific object at a specific point in time. Variables are used when the variable appears in the expression elbow, it is immediately replaced by the currently referenced object, regardless of the type of object. In addition, all variables must be explicitly assigned before they are used, and using unassigned variables produces an error. In summary, a variable is created when it is assigned, it can reference any type of object, and must be assigned before the reference. For example, when we say:>>> a = 3 at least conceptually, P Ython will perform three different steps to complete the request. These steps reflect all the assignments in the Python language: 1 Create an object to represent the value 3. 2. Create a variable A, if it has not yet been created. 3. Connect the variable to the new object 3. The actual effect is an internal structure in Python. , variables and objects are stored in different parts of memory and are associated with the connection (the connection is shown as an arrow in the diagram). Variables are always connected to the object and never connect to other variables, but larger objects may connect to other objects (for example, a list object can connect to the objects it contains). Variable name and object after running a=3. Variable a becomes a reference to object 3. Internally, a variable is actually a pointer to the object's memory space (created by running the constant expression 3)-the connection from the variable to the object in Python is called a reference. In other words, a reference is a relationship that is implemented in the form of pointers in memory. Once the variable is used (that is, it is referenced), Pyth on automatically follows the variable to the object's connection. This is actually much simpler than what the term describes. In specific terms: A variable is an element of a system table with an empty alarm for a connection to an object. Objects are allocated pieces of memory and have enough space to represent the values they represent. A reference is an automatically formed pointer from a variable to an object. Technically speaking,, the object has a more complex structure than simply having enough space to represent its value. Each object has two standard header information: A type marker that identifies the type of the object, and a reference counter that determines whether the object can be reclaimed. Type belongs to an object, not a variable in order to understand how the object type is used, look at the result when we assign a variable multiple times:>>> a = 3 # one ' s an inleger>>> a = ' spam ' # nolv il ' S a Slring>>> a = 1.23 # now IR's (1 jloaring poinr in P Ython, the situation is simple: The variable name does not have a type. As we said before, the type belongs to the object, not the variable name. In the previous example, we just changed a to a reference to a different object. Because the variable has no type, we don't actually change the type of the variable a, just let the variable refer to different types of objects. In fact, a Python variable is a specific object that is referenced at a specific time. Note that the type in Pythod is associated with the object, not the variable. In typical code, a given variable tends to refer to only one type of object. In spite of this, because this is not necessary, you will find Python code more flexible than your usual code: If you use Python correctly, your code will automatically work in multiple types. The two header information for the object, one is the type identifier and the other is the reference counter. In order to understand the latter, we need to continue to learn the following and briefly describe what has changed at the end of the object's life. Write some code to understand the content. The object's garbage collection shares reference shared references and modify shared references and equality in situ 1. Consider the following three statements. Do they change the value a prints out? A = "spam" B = AB = "Shrubbery" 2. Consider the following three statements. Do they change the value of a? A = ["spam"]b = ab[0] = "shrubbery" 3. Consider the following three statements. Do they change the value of a? A = ["spam"]b = a[:]b[0] = "shrubbery" 1. No: A will still be printed as "p a M". When B is assigned to the string "shrubbery", what happens is that the variable B is reset to point to the new string object. A and B are initially shared (that is, referencing or pointing to) the same string object "spam", but in Python these two variable names are never concatenated. Therefore, setting B for another different object has no effect on a. Such asThe same thing happens when the last statement here changes to B = Plus ' shrubbery '. In addition, the merge operation creates a new object as its result and assigns the value to B only. We'll never be there. A string (number or tuple) is overwritten because the string is immutable. 2. Yes: A is now printed as ["shrubbery"]. Technically, we have neither changed a nor changed B, we have changed the part of the object that these two variables co-reference (point to), through the variable: B overwrites the part of the object in its place. Because A, like B, refers to the same object, this change also has an effect on a. 3. No: A will still print as ["Pam"]. Since the Shard expression statement will create a copy before it is assigned to B, this time it will not affect the assignment of B in place. After the second assignment, there are two different list objects that have the same values (in Python, we say they are--but they are not). The third assignment statement changes the list object pointing to B without changing the list object that points to a.

Python notes 2 dynamic types of Python 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.