Name and meaning - Go vs Python

In python with code below

x = 3
x='str'

      

allows x

you to first point to an object of type int

and then to an object of type str

, because python is dynamically typed . type(x)

gives a value type ( 3

or str

) , but not a name type x

.

In fact, it x

does not store a value 3

, but points to an object of type int

whose value is3


In GO language with below syntax

func main() {
        y := 2
        fmt.Println(reflect.TypeOf(y)) // gives 'int'
        y = "str" // Compile time error, because GO is static typed 
}

      

Question:

Is the int

name y

type or value type 2

?

+3


source to share


1 answer


Python variables are bound to class instances that are dynamically assigned to them during the program. Therefore, and especially with mutable objects, they are simply pointers that contain information about their data location and the type of data they point to. So by assigning a new value, you are creating a new instance (which is your main interest) and associating a variable name with it, so the type is associated with the value, not the variable itself.

>>> x = 3; id(x)
1996006560
>>> x = 'str'; id(x)
1732654458784

      




Go variables, on the other hand, serve (when not pointers) as mainstay memory locations because the language is compiled and variables are given a permanent "job" to store a certain type of information (which could be a pointer like Well). Hence, the variable will almost certainly retain its memory along the program, will have constant data type properties, and you could say that the variable itself is of a certain type (not a half-pointer type).

package main
import . "fmt"
func main () {
    x := "str"; Println(&x)         // 0xc04203a1c0
    x = "Hello world!"; Println(&x) // 0xc04203a1c0
}

      

+5


source







All Articles