A small problem with go and Python variable assignment

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Usually write much is python, recently saw a little go, today encountered a problem, and share with you

package mainimport "fmt"type student struct {    Name string    Age  int}func pase_student() {    m := make(map[string]*student)    stus := []student{        {Name: "zhou", Age: 24},        {Name: "li", Age: 23},        {Name: "wang", Age: 22},    }    for _, stu := range stus {        m[stu.Name] = &stu    }    fmt.Println(m["zhou"].Name)}func main() {    pase_student()}

The code is simple, so you can think about what you're going to print out.

time.Sleep(60)  # 思考

The result is wang !, surprise not surprise! Traverse assignment Ah, students, such a simple operation can be a moth, wtf!

Why is it wang ? you TM explain to me What a surprise is :

For loop, the pointer to the variable Stu is constant, and each loop is merely a copy of the value of the student struct, and the for loop above is the same as the following:

var stu student for _, stu = range stus {m[stu.Name] = &stu}

So &stu it's always an address that changes the value stored on that address. The &stu final stored value is the student{Name: "wang", Age: 22} struct, so what is taken out is wang .

You can take a m look at:

map[zhou:0xc42000a260 li:0xc42000a260 wang:0xc42000a260]

The idea above is verified, and everyone's value is the same address.

See here, if is a daily writing c,c++ and other strong type language classmate may say, neuropathy Ah! There's nothing to say! That's the way it is! Please forgive me, I write Python's [cover face] everyday.

As can be seen from the above example, in go, the variable name is 存储地址的名字 . It has been completed at compile time and cannot be changed at runtime, you can only change the value stored in the address.

In Python, the variable is the name of the object, and the run-time variable can be bound to any object. As shown below:

In [4]: a = 123456In [5]: id(a)Out[5]: 4426596208In [6]: a = 1234567In [7]: id(a)Out[7]: 4426592592

Note: Since Python implements a small integer object pool for int types, do not experiment with 0-255 integers, otherwise you will get the same ID.

In other words, when you loop a list, each time you get a different object, the variable points to a different address:

In [9]: for i in [2222, 2223, 2224]:   ...:     print(id(i))   ...:442659620844265923364426596080

The above code, Python created 3 for us PyIntObject ,i just their name. In go, you can think of only one object, and the value changes 3 times.

Python says 赋值就是建立一个对象的引用 , is the truth.

Related Article

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.