| |

VerySource

 Forgot password?
 Register
Search
View: 4055|Reply: 11

Immutable variables in python

[Copy link]

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-23 16:30:01
| Show all posts |Read mode
Why are there immutable variable types? ? Isn't this troublesome?

Such as:
num = 1
num = num + 2

In the above simple sentence, an int object is generated twice. Isn't this slower using python? ?

Is it slower to modify the value of a variable than to generate a variable? ?

Generating a variable is not more of a tool to do. Don't understand
Reply

Use magic Report

0

Threads

1

Posts

1.00

Credits

Newbie

Rank: 1

Credits
1.00

 United States

Post time: 2020-5-23 13:03:24
| Show all posts
A variable is a bunch of addresses in the RAM, changing the value is changing the value of whatever is stored in the bunch of addresses. Generating a variable is saving RAM storage for that variable, then your code can put information in those storage spaces in RAM.
Reply

Use magic Report

0

Threads

36

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-7-3 09:15:01
| Show all posts
In C++, there is this situation
int a = 1;
a = a++ + ++a;
Variables are immutable or can avoid ambiguity
Reply

Use magic Report

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-7-4 22:30:01
| Show all posts
Are there any other benefits? It is too much for this sacrifice. :)
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-14 21:00:01
| Show all posts
>> In the simple sentence above, two int objects were generated. Isn't this slower with python? ?
It’s not obvious whether you’re slow or not, just try it and you’ll know.

>> Could it be slower to modify the value of a variable than to generate a variable? ?

Immutable objects have many benefits, the most obvious is object sharing:

str1 = "abc"
str2 = "abc"
str3 = "abc"

Because "abc" is immutable, now only one "abc" object is needed.

If "abc" is variable, when str1 is modified, str2 and str3 must have a copy of "abc" independent of str1 in order not to be affected, so three objects are required.

And some applications require immutable data, such as dictionary keys. If you have learned hashing, you should be able to understand.
Reply

Use magic Report

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 United States

 Author| Post time: 2020-7-15 12:00:02
| Show all posts
People who come from C/C++, I want to learn python well, it is important to have two steps:

Immutable variables.

There are no character variables.

Not right, right? ?
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-16 09:00:01
| Show all posts
>> Immutable variables.
I don’t think it’s difficult to understand that immutable classes are often written in C++.

>> There is no character variable.
It's really a bit uncomfortable.
Reply

Use magic Report

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-7-16 17:15:01
| Show all posts
I have always used C, and have never used C++ (although I have studied, I have only learned, I have not used it.)


There is no such thing as an immutable variable in C. It's a little awkward for me.
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-7-16 23:00:01
| Show all posts
Similar to const.
Reply

Use magic Report

3

Threads

13

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-7-18 00:15:02
| Show all posts
I now use const to understand it.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list