变量存在 [英] variables exist

查看:106
本文介绍了变量存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中测试变量是否存在于php中的isset?


感谢

how testing if a variable exists in python as isset in php??

thanks

推荐答案

"费边"写道:
"fabian" wrote:
如何测试python中的变量是否存在于php中的isset?
how testing if a variable exists in python as isset in php??




尝试:

变量

除了NameError:

print" variable not set"


但这真是糟糕的Python;更好地确保你总是将所有变量分配给

,并使用None(或其他合适的值)来标记某些

变量现在没有任何有意义的内容。


,而不是


如果条件:

变量= 1


...


试试:

变量

除了NameError:

... 。


写的


变量=无


如果条件:

变量= 1


...


如果变量为None:

print" variable not not设置


< / F>



try:
variable
except NameError:
print "variable not set"

but that is really lousy Python; better make sure you always assign to
all variables, and use None (or another suitable value) to mark that some
variable has no meaningful content right now.

that is, instead of

if condition:
variable = 1

...

try:
variable
except NameError:
....

write

variable = None

if condition:
variable = 1

...

if variable is None:
print "variable not set"

</F>


尝试:

myVariable

除了NameError:

print" Not bound"

else:

print" Bound"


如果你想区分本地和全球环境:


如果globals()。has_key(" myVariable"):

......


vs.


if locals()。has_key(" .....

HTH,

Gerald


fabian schrieb:
try:
myVariable
except NameError:
print "Not bound"
else:
print "Bound"

If you want to distinguish between the local an the global environment:

if globals().has_key( "myVariable" ):
...

versus

if locals().has_key( ".....
HTH,
Gerald

fabian schrieb:
如何在python中测试一个变量是否存在于php中?

谢谢
how testing if a variable exists in python as isset in php??

thanks




-

GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634



--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634

Fredrik Lundh在2005-04-11 10:14对世界说:
Fredrik Lundh said unto the world upon 2005-04-11 10:14:
" fabian"写道:
"fabian" wrote:
如何在python中测试一个变量是否存在于php中?

尝试:
变量
除了NameError:
打印变量未设置

但这真是糟糕的Python;更好地确保你总是分配给所有变量,并使用None(或其他合适的值)来标记某些
变量现在没有任何有意义的内容。

即而不是
how testing if a variable exists in python as isset in php??

try:
variable
except NameError:
print "variable not set"

but that is really lousy Python; better make sure you always assign to
all variables, and use None (or another suitable value) to mark that some
variable has no meaningful content right now.

that is, instead of




< SNIP完全尝试/除了示例>



变量=无

如果条件:
变量= 1


如果变量为None:
print" variable not set"

< / F>



<SNIP full try/except example>
write

variable = None

if condition:
variable = 1

...

if variable is None:
print "variable not set"

</F>



嗨Fredrik和所有人,


我是一个爱好者,还在学习但是,尝试/除外的声明是

糟糕的Python让我大吃一惊。 try / except方式看起来像是更容易请求宽恕而不是权限的实例,而我的感觉

是95%的情况下,这种方法是广泛的考虑更多

Pythonic比LBYL(其中建议的成语似乎让人想起)。


实际上,我的大多数代码都将预设名称设置为None,但是阅读

这个帖子让我很奇怪。你是否建议设置为None

更好,因为它明确表示没有任何内容,但这可能会在未来改变
? (这就是我这样做的原因。)或者出于其他原因?


我没有任何党派利益;我只是想要了解b
。非常感谢,


Brian vdB


Hi Fredrik and all,

I''m a hobbyist and still learning, but the claim the try/except is
"lousy Python" surprise me a bit. The try/except way seems like an
instance of "Easier to ask Forgiveness than Permission", and my sense
is that in 95% of cases that approach is widely considered more
Pythonic than LBYL (of which the suggested idiom seems reminiscent).

As it happens, most of my code does preset names to None, but reading
this thread made me wonder. Are you suggesting setting to None is
better as it makes explicit that there is nothing there, but this may
change in the future? (This is why I do it.) Or for some other reason?

I have no partisan interests either way; I''m just trying to
understand. Thanks and best,

Brian vdB


这篇关于变量存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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