所以,我想检查并验证给定的变量“abc”是否存在并且它是真的。如果变量存在并且为 False,那么我希望它转到其他位置。这是我如何让它在 python 中工作:
env = os.environ.copy()
if "abc" in env and env['abc'] == "True":
print "Works"
else:
print "Doesn't work"
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
我想在一个循环中运行完整的 Jupyter 笔记本,为笔记本的每次运行传递不同的参数。我可以使用插件传递参数,如下所述: 将命令行参数传递给 jupyter/ipython 笔记本中的 argv,但这似乎过于麻烦。有没有更简单直接的方法来做到这一点?
查看python中是否存在列表或dict的最简单方法是什么?
我使用以下但这不起作用:
if len(list) == 0:
print "Im not here"
Run Code Online (Sandbox Code Playgroud)
谢谢,
我目前正在使用jupyter笔记本,我想删除仅在单元格内使用的变量,这样我就不会意外地在其他单元格中滥用这些变量。
例如,我想删除以下代码中的变量myvar和循环变量:i
start = 1
stop = 2
for i in range(start, stop):
pass
myvar = "A"
# other statements
# ...
del i, myvar # Runs ok, and variables i and myvar are deleted and I won't accidentally use i or myvar in another jupyter notebook cell
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但在某些情况下,某些变量实际上没有定义。在下面的示例中,这会引发错误,因为i未定义,因为循环从未运行。定义的变量myvar不会被删除。
start = 1
stop = 1 # Now we have stop equal to 1
for i in range(start, stop):
pass
myvar = "A"
# …Run Code Online (Sandbox Code Playgroud) 我有相同的代码,我偶尔从命令行运行它,python source.py偶尔将其复制/粘贴到交互式 IPython 中。我想以两种方式执行稍微不同的代码,并尝试将差异嵌入到以下代码块中:
try:
__IPYTHON__
# do IPython-mode code
except NameError:
# do script-mode code
Run Code Online (Sandbox Code Playgroud)
我知道在 Python 中这是一种常见的技术,但它搞砸了 PyCharm 中的自动标记,我希望找到一些更有吸引力的方法来测试 IPython 的存在,也许通过测试'__IPYTHON__' in ...或类似的方法。然而,我看到这个符号既不在locals()也不在 中globals()。有没有其他地方可以测试它的存在,或者任何其他更常规的测试,而不直接使用异常?
我正在使用 Python 2.7.11 和 IPython 5.1.0。非常感谢。
更新相关但无帮助:How do I check if a variable isn’t?
我正在阅读一百多万个文件来删除一些数据.这些文件通常非常统一,但偶尔也会出现我希望找不到的东西.
例如,我希望一些sgml代码能够识别我需要的值
for data_line in temp #temp is a list of lines from a file
if <VARIABLENAME> in data_line:
VARIABLE_VAL=data_line.split('>')[-1]
Run Code Online (Sandbox Code Playgroud)
后来我用了VARIABLE_VAL.但我有时会得到一个例外:文件中没有行
<VARIABLENAME>theName
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我在处理完所有行后添加了这一行:
try:
if VARIABLE_VAL:
pass
except NameError:
VARIABLE_VAL=somethingELSE
Run Code Online (Sandbox Code Playgroud)
我已经看到了某个地方(但我找不到了)一个看起来像的解决方案
if not VARIABLE_VAL:
VARIABLE_VAL=somethingELSE
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激
在处理许多 if/elif/else 时,如果已经声明了一个变量,我有时会丢失跟踪。我想知道一种检查变量 foo 是否已经声明的简单方法。实现这一目标的最简单语法是什么?
Ps 看看如何使用 globals() 和 locals() 来实现这一点很有趣。
我最终使用了:
if not 'myVariableName' in globals().keys(): myVariableName='someValue'
Run Code Online (Sandbox Code Playgroud)
很难跟踪何时何地/如果app = QtGui.QApplication(sys.argv)已经声明,尤其是在一个 gui 模块调用另一个模块时,反之亦然。
如果 globals() 中的“app”(似乎)有助于避免意外的变量重新声明。
这样可以正常工作:
a = 1
if a:
b = a
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
if a:
b = a
Run Code Online (Sandbox Code Playgroud)
鉴于我们明确表示,它不会像执行此"if"语句一样执行.
"if a exists"
Run Code Online (Sandbox Code Playgroud)
那为什么会出错呢?如果它不存在则只是在if语句的参数内不做任何事情.
UPDATE
事实证明"如果一个"意味着......"如果一个值"意味着在python中.
我正在寻找"如果一个存在然后前进"的等价性
如果存在"x",则打印"x exists".
我问这个因为我总是得到这个错误:
UnboundLocalError at /settings/
local variable 'avatarlink' referenced before assignment
Run Code Online (Sandbox Code Playgroud) 我想在Python中写一个像这个javascript的语句:
if (variable1 && variable2) {
// do something only if both variables exist
}
我尝试着:
if not (variable1 and variable2) is None:
# do something only if both variables exist
但它不起作用...当我调试时,变量2没有定义,但函数仍然试图运行.出了什么问题?
对于原始类型,我可以使用if in:boolean check.但是如果我使用in语法来检查类成员的存在,我会得到一个NameError异常.有没有办法在Python中检查没有异常?或者是尝试除了块之外的唯一方法吗?
这是我的示例代码.
class myclass:
i = 0
def __init__(self, num):
self.i = num
mylist = [1,2,3]
if 7 in mylist:
print "found it"
else:
print "7 not present" #prints 7 not present
x = myclass(3)
print x.i #prints 3
#below line NameError: name 'counter' is not defined
if counter in x:
print "counter in x"
else:
print "No counter in x"
Run Code Online (Sandbox Code Playgroud) 这是一个相关的问题:如何检查变量是否存在?
但是,它对静态变量不起作用.
我想要做的是以下,
class A:
def __init__(self):
if A.var is null: # this does not work, okay
A.var = 'foo'
print 'assigned'
Run Code Online (Sandbox Code Playgroud)
好的,因为A.var甚至没有分配.它引起了错误.所以,我试过这个:
class A:
def __init__(self):
if 'A.var' not in globals(): # this seems to okay, but ..
A.var = 'foo'
print 'assigned'
a = A()
b = A()
Run Code Online (Sandbox Code Playgroud)
结果如下:
assigned
assigned
Run Code Online (Sandbox Code Playgroud)
这表明该if 'A.var' not in globals():行无法正常工作.
那么,我如何检查Python中是否存在静态变量?
python ×12
ipython ×2
python-2.7 ×2
variables ×2
debugging ×1
declaration ×1
object ×1
python-3.x ×1