del class with wxpython inside conditional 工作意外

del class with wxpython inside conditional works unexpectedly

我正在尝试在条件完成时删除 class 的一个实例。 但是我有问题,因为它在进入条件之前被删除了。 我不知道发生了什么... 代码使用 wxpython 和一些用于删除项目的按钮,所以我在 init 上正确构建了按钮但是当我尝试删除项目时,在它到达第一个条件之前,它似乎被 las 条件删除,永远不要这样做前。 所以我不知道问题出在哪里...... 我第一次按下按钮 'deleteitem' 时得到的错误是:

'赋值前引用的局部变量'T''(第6行:... if T.items>0:)

但是如果我删除最后一行 del(T) 它不会给出任何错误。

基本代码如下:

class Test(object):
    def __init__(self):
        self.items=8

T=Test()



if button.GetName()=='deleteitem': 
    if T.items>0:
        T.items-=1
        if T.items<0:
            del(T)

已编辑:

好的,因为我首先发布的示例可以运行,下面是不起作用的代码:

import wx

class Test(object):
    def __init__(self):
        self.items=8

T=Test()

class MyFrame(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title,
                          pos=(150, 150), size=(350, 200))

        self.btn = wx.Button(self, -1, "Press to delete Item, current Items: "+str(T.items))
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.btn)


    def OnButton(self, evt):
        print 'Current Items: '+str(T.items)
        self.btn.SetLabel('Press to delete Item, current Items: '+str(T.items))
        if T.items>0:
            T.items-=1
            if T.items==0:
                del(T)


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, "Simple wxPython App")
        frame.Show(True)
        return True

app = MyApp()
app.MainLoop()

最终工作代码:

import wx

class Test(object):
    def __init__(self):
        self.items=8

class MyFrame(wx.Frame):

    def __init__(self, parent, title):
        self.T=Test()
        wx.Frame.__init__(self, parent, -1, title,
                          pos=(150, 150), size=(350, 200))

        self.btn = wx.Button(self, -1, "Press to delete Item, current Items: "+str(self.T.items))
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.btn)


    def OnButton(self, evt):
        if self.T.items>0:
            self.T.items-=1
            if self.T.items==0:
                del(self.T)
                self.btn.SetLabel('Deleted instance T')
            else:
                self.btn.SetLabel('Press to delete Item, current Items: '+str(self.T.items))
                print 'current Items: '+str(self.T.items)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, "Simple wxPython App")
        frame.Show(True)
        return True

app = MyApp()
app.MainLoop()

您遗漏了一些相关代码,因为此代码:

class Test(object):
    def __init__(self):
        self.items=8

T=Test()



if True: 
    if T.items>0:
        T.items-=1
        if T.items<0:
            del(T)

执行无误。

无论如何,听起来您的代码是 Variable scope in nested functions.

的某种变体

响应新代码:

据我所知,del 本地化了一个变量:

示例 1:

class Test(object):
    def __init__(self):
        self.items=8

T = Test()

class Dog(object):
    def dostuff(self):
        print 'executing dostuff()'
        if T.items > 10:  #This will never be True
            T

Dog().dostuff()

--output:--
executing dostuff()

示例 2:

class Test(object):
    def __init__(self):
        self.items=8

T = Test()

class Dog(object):
    def dostuff(self):
        print 'executing dostuff()'
        if T.items > 10:  #This will never be True
            del T


Dog().dostuff()

--output:--
executing dostuff()
Traceback (most recent call last):
  File "1.py", line 14, in <module>
    Dog().dostuff()
  File "1.py", line 10, in dostuff
    if T.items > 10:
UnboundLocalError: local variable 'T' referenced before assignment

所以看起来解析器将 T 标记为 dostuff() 函数内的局部变量——因为您只能 del 局部名称(除非您在 dostuff() 内声明 T 为全局变量)。因此,python 不会在 dostuff() 函数之外查找 T 的值。

因此,另一种解决方案(不推荐)是这样做的:

class Test(object):
    def __init__(self):
        self.items=8

T = Test()

class Dog(object):
    def dostuff(self):
        global T  #<===HERE
        print 'executing dostuff()'

        if T.items > 10:
            del T


Dog().dostuff()

--output:--
executing dostuff()

为了解决您的问题,我会将以下内容添加到您的 __init__() 函数的末尾:

self.T = Test()

或者,您可以将 T 作为参数传递给 __init__()。通常认为在 class 中有函数来操纵全局变量是糟糕的程序设计。