簡體   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