欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python之常量和变量

程序员文章站 2022-07-04 09:28:25
局部和全局变量: 1 # name='lhf' 2 # def change_name(): 3 # # global name 4 # name='帅了一比' 5 # print('change_name',name) 6 # 7 # 8 # change_name() 9 # print(nam ......

局部和全局变量:

python之常量和变量
  1 # name='lhf'
  2 # def change_name():
  3 #     # global name
  4 #     name='帅了一比'
  5 #     print('change_name',name)
  6 #
  7 #
  8 # change_name()
  9 # print(name)
 10 
 11 # name='lhf'
 12 #
 13 # def change_name():
 14 #     name1='帅了一比'
 15 #     name2='帅了一比'
 16 #     name3='帅了一比'
 17 #     print('change_name',name)
 18 #
 19 # change_name()
 20 # globals
 21 
 22 # NAME = "杠娘"
 23 #
 24 # def yangjian():
 25 #     # NAME = "史正文"
 26 #     global NAME
 27 #     NAME = "小东北"
 28 #     print('我要搞', NAME)
 29 
 30 
 31 #
 32 # def qupengfei():
 33 #     NAME = "基"
 34 #     print('我要搞', NAME)
 35 #
 36 # yangjian()
 37 # qupengfei()
 38 
 39 
 40 
 41 # NAME = "产品经理"
 42 
 43 # def yangjian():
 44 #     # NAME = "史正文"
 45 #     global NAME # 已经声明,NAME就是全局的的那个变量
 46 #     print('我要搞', NAME)
 47 #     NAME = "小东北"  # 修改 全局的变量
 48 #     print('我要搞', NAME)
 49 
 50 
 51 
 52 # def qupengfei():
 53 #     NAME = "基"
 54 #     print('我要搞', NAME)
 55 
 56 
 57 
 58 # 如果函数的内容无global关键字,
 59 #   - 有声明局部变量
 60 # NAME = ["产品经理","廖波湿"]
 61 # def qupengfei():
 62 #     NAME = "自己"
 63 #     print('我要搞', NAME)
 64 # qupengfei()
 65 #   - 无声明局部变量
 66 # NAME = ["产品经理","廖波湿"]
 67 # def qupengfei():
 68 #     NAME.append('XXOO')
 69 #     print('我要搞', NAME)
 70 # qupengfei()
 71 
 72 # 如果函数的内容有global关键字
 73 #   - 有声明局部变量
 74 # NAME = ["产品经理","廖波湿"]
 75 # def qupengfei():
 76 #     global NAME
 77 #     NAME = "自己"
 78 #     print('我要搞', NAME)
 79 # qupengfei()
 80         # 错误示例
 81 # NAME = ["产品经理","廖波湿"]
 82 # def qupengfei():
 83 #     NAME = "自己"
 84 #     global NAME
 85 #     print('我要搞', NAME)
 86 # qupengfei()
 87 #   - 无声明局部变量
 88 # NAME = ["产品经理","廖波湿"]
 89 # def qupengfei():
 90 #     global NAME
 91 #     NAME = ["阿毛"]
 92 #     NAME.append('XXOO')
 93 #     print('我要搞', NAME)
 94 # qupengfei()
 95 
 96 ######## 全局变量变量名大写
 97 ######## 局部变量变量名小写
 98 
 99 
100 # 优先读取局部变量,能读取全局变量,无法对全局变量重新赋值 NAME=“fff”,
101 #     但是对于可变类型,可以对内部元素进行操作
102 # 如果函数中有global关键字,变量本质上就是全局的那个变量,可读取可赋值 NAME=“fff”
103 # qupengfei()
104 # yangjian()
105 
106 
107 NAME = ["产品经理","廖波湿"]
108 
109 # def yangjian():
110 #     # NAME = "史正文"
111 #     global NAME # 已经声明,NAME就是全局的的那个变量
112 #     print('我要搞', NAME)
113 #     NAME = "小东北"  # 修改 全局的变量
114 #     print('我要搞', NAME)
115 
116 # def qupengfei():
117 #     # NAME = "aS"
118 #     NAME.append('天扎龙')
119 #     print('我要搞', NAME)
120 #
121 # qupengfei()
122 
123 
124 # NAME = ["产品经理","廖波湿"]
125 # def qupengfei():
126 #     name = "自己"
127 #     global NAME
128 #     print('我要搞', NAME)
129 # qupengfei()
130 
131 # NAME = '海风'
132 #
133 # def huangwei():
134 #     name = "黄伟"
135 #     print(name)
136 #     def liuyang():
137 #         name = "刘洋"
138 #         print(name)
139 #         def nulige():
140 #             name = '沪指花'
141 #             print(name)
142 #         print(name)
143 #         nulige()
144 #     liuyang()
145 #     print(name)
146 #
147 # huangwei()
148 
149 # name = "刚娘"
150 # 
151 # def weihou():
152 #     name = "陈卓"
153 #     def weiweihou():
154 #         nonlocal name   # nonlocal,指定上一级变量,如果没有就继续往上直到找到为止
155 #         name = "冷静"
156 # 
157 #     weiweihou()
158 #     print(name)
159 # 
160 # print(name)
161 # weihou()
162 # print(name)
163 # 刚娘
164 # 冷静
165 # 刚娘
View Code