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

Go basic variable scope comparison Python

編輯:Python

Pay attention to differences go and python The variable scope of .go It's a static variable ,python It's a dynamic variable . Usually we are in python Global variables are not used in .

go Variable scope demonstration :

go

package main
import "fmt"
var a int = 20 // Defining global variables requires var Statement , Global variables define that you can not use
func main() {
// Scope of variable
c := 10 // Local variables defined inside functions
fmt.Println(c)
println(" Global variables a", a)
// Local variables can have the same name as global variables
// Local variables are preferred inside functions
a := "a"
println(" local variable a", a)
}

Running results

 

python Variable scope demonstration

python


a = 10
def fun():
a = "string"
print(a)
def fun2():
global a
a = "20"
print(a)
if __name__ == '__main__':
fun() # a = "string"
print(a) # a = 10
fun2() # a = 20
print(a) # a =20

Running results :

 


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