Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决';未定义全局名称self';?_Python_Ironpython - Fatal编程技术网

Python 如何解决';未定义全局名称self';?

Python 如何解决';未定义全局名称self';?,python,ironpython,Python,Ironpython,此简单代码正在侦听C:上的更改: import clr clr.AddReference('System.IO') from System.IO import (DriveInfo) from WatchDir import WatchedItem from MobileNotifier import MobileNotifier class WinWash(object): ''' Watches the server for important changes. ''' d

此简单代码正在侦听C:上的更改:

import clr
clr.AddReference('System.IO')
from System.IO import (DriveInfo)
from WatchDir import WatchedItem
from MobileNotifier import MobileNotifier

class WinWash(object):
    ''' Watches the server for important changes. '''
    def __init__(self, notifier):
        self.notifier = notifier
        self.min_size = 20000

    def on_low_space(self, sender, event):
        ''' Notifies if free space is below min_size '''
        my_drives = DriveInfo.GetDrives()
        for drive in my_drives:
            megabytes_free = (drive.AvailableFreeSpace / 1000000)

            if megabytes_free < self.min_size:
                self.notify('Disk.Space-{0}-{1}.MB'.format(drive.Name, megabytes_free))
                print 'Sent!'

    def notify(event, message):
        ''' Send message to user. '''
        app = 'WinWash'
        self.notifier(app, event, message)

def main():
    notifier = MobileNotifier()
    win = WinWash(notifier)
    disk = WatchedItem('C:\\')
    # callback/event handler
    disk.watch_create(win.on_low_space)

    choice = ''
    while choice != 'q':
        choice = raw_input("Press 'q' to exit: \n")
我猜回调有问题,因为当引发
事件时(文件是在C:上创建的),我会收到这个错误:


我对此感到困惑,我不确定如何避免
self
,也不确定是哪个
self
导致了问题。

您在类
WinWash
中的方法通知声明中没有包含self:

def notify(event, message):
    ''' Send message to user. '''
    app = 'WinWash'
    self.notifier(app, event, message)
应该是:

def notify(self, event, message):
    ''' Send message to user. '''
    app = 'WinWash'
    self.notifier(app, event, message)

def notify(事件、消息):
…@jornsharpe最简单的事情。。。v_vdamn,我觉得很傻v_v抱歉错过了这个。。。我会把你标记为正确的。@bbe没问题,有时候需要几个小时没有看的眼睛。
def notify(event, message):
    ''' Send message to user. '''
    app = 'WinWash'
    self.notifier(app, event, message)
def notify(self, event, message):
    ''' Send message to user. '''
    app = 'WinWash'
    self.notifier(app, event, message)