获取全局名称未定义错误

1 投票
2 回答
5941 浏览
提问于 2025-04-15 22:42

我有一个这样的类:

class notify():
    def __init__(self,server="localhost", port=23053):
        self.host = server
        self.port = port
        register = gntp.GNTPRegister()
        register.add_header('Application-Name',"SVN Monitor")
        register.add_notification("svnupdate",True)
        growl(register)    

    def svn_update(self, author="Unknown", files=0):  
        notice = gntp.GNTPNotice()
        notice.add_header('Application-Name',"SVN Monitor")
        notice.add_header('Notification-Name', "svnupdate")
        notice.add_header('Notification-Title',"SVN Commit")
        # notice.add_header('Notification-Icon',"")
        notice.add_header('Notification-Text',Msg)
        growl(notice)

    def growl(data):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((self.host,self.port))
        s.send(data)
        response = gntp.parse_gntp(s.recv(1024))
        print response
        s.close()    

但是每当我尝试通过以下代码使用这个类时,我会遇到一个错误:NameError: global name 'growl' is not defined

from growlnotify import *
n  = notify()
n.svn_update()

有没有人知道这是怎么回事?

谢谢,
nash

2 个回答

1

growl 不是一个全局的符号,它是 notify 类的一个成员。

notify 类里面,调用 growl 方法的方法如下:

self.growl(notice)
3

在Python中,实例的作用域不会被自动查找。如果你想在self上调用一个方法,你必须在方法前面加上self的引用。

self.growl(register)

撰写回答