1

我有以下课程

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()

任何人都知道这里发生了什么?

干杯纳什

4

2 回答 2

3

在 Python 中,实例范围不会作为范围解析的一部分进行搜索。如果你想调用一个方法,self那么你必须在它前面加上对self.

self.growl(register)
于 2010-05-14T16:14:23.857 回答
1

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

notify类内部,调用growl方法如下:

self.growl(notice)
于 2010-05-14T16:15:52.067 回答