Python 如何解决';未定义全局名称self';?
此简单代码正在侦听C:上的更改: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
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)