Comparison of two variable values written in Python, C + + and not using temporary variables

Source: Internet
Author: User

In C + +, the value of exchanging two variables is usually the first reaction is the following code:

{Temp=a;a=b;b=temp}

But does the topic require temporary variable temp to be used? Maybe you need to think about it for a while. But there are also solutions:

{    b=a+b;    a=b-a;    B=b-a;}

This is true if the values of a and B are small, but the a+b is too large to overflow, so it is not an optimal solution. The best approach is to use XOR:

{A ^= bb ^=aa ^=b}



This problem is too retarded in Python!

A,b=b,a

A straight line is done!



What if the topic asks for a function to exchange values for two variables?

In C + + We all know that you need to pass a pointer or reference as a parameter to implement the value of the interchange two variables, as follows:

void Swap (int*a,int* b) {int temp=*a;*a=*b;*b=temp;} int A=3;int B=4;swap (&a,&b);
Or a simpler use of references:

void swap (int &a,int &b) {int t=a;a=b;b=t;} Swap (A, b);

But it's not possible in Python. Python's mechanism is somewhat difficult (when it comes to contact), it is a weak type of language, all variables can be used without declaring the type directly, can also be understood as a variable is only a memory object of a "point", the variable is only a symbol, there is no practical significance, the real type concept is the memory object.

Python can be divided into variable objects (such as list, dict) and immutable objects (strings, numbers, tuples) in many types of objects. Immutable objects understand that you can refer to a string in C + +, like there is a string pool, you cannot change the contents of a string, but when you assign a value, you simply pick a different string from the string pool to assign the variable. Mutable objects, as the name implies, are objects that can change values, for example:

a=2

B=[2]

The effect of these two sentences is to first create an integer object in memory with a value of 2, and then make the variable a point to the object. The second sentence is to create a list of lists, then the first element of the list points to the object just in memory, "2", and variable B points to the created list. We can use the ID () to see where the object is stored in memory.

You can see that a and b[0] actually point to the same area of memory. This is in memory in fact there is only a value of 2 of the object, no matter how many variables are assigned to 2, actually all just point to this object, those variables just play a point of action, and this 2 is not able to change, that is, during this run, the memory area of the value of the store is unchanged. List is not the same, the contents of the list can be changed.

A=5b[0]=5

After executing these two sentences, id (a) and ID (b[0]) are the same as the original? The answer is NO! Look at the results:


A=5 just creates an integer object with a value of 5 and points the variable A to it, and Object 2 is still in its original position and does not change. Careful observation will find that the position of B has not changed and become just the content, so the list is a mutable object.

To clarify these, in the process of parameter passing of the function, if only a variable that points to an integer is passed, then only a few new variables are created, and any modification of the parameters in the function will only change the direction of the new variable, without affecting the original variable in the slightest.


To make it more clear, the parameter name we pass in the function is still a, but the A and the outside are not half-dime, it's just a sign, and the b,c,d are the same, without any real meaning. As you can see, before calling the function, A has pointed to the object "2", and when the function is called, the object's point to this is passed to the parameter A in the function, so a points to a memory of 4893164, which is the address of memory 2 in the previous article. Later assigned a value of 5 and look at the address is also no problem. When this function ends, the life of the variable A is over, and once the function is exited, it is still pointing to object 2 when it looks at the value of a. This happens to indicate that a in a function has no relation to the outside of a.

So in Python if you want to swap the values of two variables with a function, I don't have a good idea, maybe only by passing in a Mutable object like list.

Comparison of two variable values written in Python, C + + and not using temporary variables

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.