如果变量不存在,如何检查实例是否存在? [英] How to check if instance exists if variable not existing?
本文介绍了如果变量不存在,如何检查实例是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮,用于导入带有类的模块. 该类(varClass)创建一个窗口.
I have a button which imports a module with a class. The class (varClass) creates a window.
如果我再次单击该按钮,请尝试以下操作:
If i click the button once again, i try this:
if var:
var.toggleUI()
else :
var = varClass()
但是打开Maya后第一次创建窗口时不存在var.我该如何工作?
But var doesn' exist the first time you create the window after opening Maya. How can i get this working?
推荐答案
您可以捕获NameError
异常:
try:
var.toggleUI()
except NameError:
var = varClass()
如果您也需要第一次拨打toggleUI
,只需尝试输入名称即可:
If you needed call toggleUI
the first time too, just try the name itself:
try:
var
except NameError:
var = varClass()
var.toggleUI()
我对Maja并不熟悉,但是如果您可以先在其他地方定义名称,然后在此处简单地将其设置为None
,那么您的代码也可以工作,即使不是更好.
I'm not familiar with Maja, but if you can define the name elsewhere first and simply set it to None
there, then your code would work too, if not better.
这篇关于如果变量不存在,如何检查实例是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文