繁体   English   中英

获取全局名称未定义错误

[英]getting global name not defined error

我有以下课程

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

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

欢呼纳什

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

self.growl(register)

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

notify类中,调用growl方法,如下所示:

self.growl(notice)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM