程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python mutable and immutable types and go value types and reference types

編輯:Python

python Variable immutable and go Compare the value type of with the reference type

python Variable and immutable types

stay python in , There is a saying that everything is an object , Everything quotes , So in python in , In fact, there is no real basic data type , Whether it's a list , character string , value type , In fact, they are objects generated by classes . That's why python in integer There is no length limit , The reason that can be infinite .

And because python Everything is an object ,python How the underlying store values , All are Variable name -> Memory address , Memory address -> data , This way of reference passing .

If python Didn't do anything , In theory , Whether it's a list or a number , character string , When modifying a value , The memory address should be unchanged . But only set , list , Dictionary three types of data that modify the value without changing the memory address , stay python Is called variable type

however , actually , If in python If you modify The number , character string , Tuples , Boolean data , Value change , The memory address will also change , This category is in python It is called immutable type data

This point , You can pass parameters in the function , It can be seen more clearly , Variable type passed in , Modify in the empty internal name of the function , The global data will also be modified , But immutable types , Modify in internal space , But it will not affect the global variables

therefore , Actually in python in , This is very strange , So people who really understand , Will not say python Function parameters are passed by reference , Or value passing , It's about quoting everything , therefore python Only then can the author distinguish between variable and immutable types . So seeing this , You just understand people

go Value type and reference type

And in the go in , There is a good distinction between the two .go Data types in can be divided into value types and reference types

Value types include : All integers 、 floating-point 、bool type 、string type 、 Array and struct type

The characteristic of value type is that variables store values directly , Memory is usually in Stack (stack) The distribution of

Reference types include : correct 、slice、map、chan、interface

The characteristics of reference types , Variable stores an address , Only the space corresponding to this address can really store data , Memory allocation is usually in Pile up (heap) The distribution of , When no variable references this address , The data space corresponding to this address will be GC Recycling -> This and python It can't be as like as two peas , It can only be said that No different

stay go in , Pass parameter to function , If the parameter type is a value type , will copy A copy is passed into the function , If it's a reference type , Just pass the parameters directly into the function ( It is also possible that both copy 了 , But a copy Value , One copy Memory address of )

Add : Whether in go In or out python in , Functions are first-class citizens , It can be directly passed to the variable name as the value of the variable . It can also be used as a function parameter


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved