如何检查Python中具有相同名称的全局变量和局部变量是否相等 [英] How to check if both global and local variable with the same name are equal in Python

查看:323
本文介绍了如何检查Python中具有相同名称的全局变量和局部变量是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def fun():

global s

print(s)

s =efg

打印



s =abc

fun()



#in fun()我需要检查全球和本地s是否相等



我尝试过:



如何检查哪一个是本地的和全局的,同时检查它们是否相等

解决方案

尝试这样的事情:

  def  fun():
全局 s
s2 = s
print global:,s)
s = efg
print local :,s,s2)
if s == s2:
print


def fun():
global s
print (s)
s="efg"
print (s)

s="abc"
fun()

#in fun() i need to check whether global s and local s are equal or not

What I have tried:

How to check which one is local and global while checkig whether they are equal

解决方案

Try something like this:

def fun():
	global s
	s2 = s
	print("global: ", s)
	s = "efg"
	print("local: ", s, s2)
        if s == s2:
                print("true")


这篇关于如何检查Python中具有相同名称的全局变量和局部变量是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆