开发者

create a global function in python

In python how can I create a function that can be global an used in all classes that are call开发者_如何学JAVAed? Here is an example:

import configparser
import os
import sys
from datetime import datetime
from ftplib import FTP

def notify(msg):
    echo = True
    log = True
    if echo:
        print(msg)
    if log:
        f = open('log.txt','a')
        msg = datetime.now().strftime("%y-%m-%d-%H:%M:%S")+': ' + msg
        f.write(msg)
        f.close()
    #sys.exit()  #removing this was the fix!

class zoneFTP():
    def __init__(self):
        self.conn = FTP()
        self.dir = './'
        notify('The dir is :' + self.dir)

def main():
    notify('starting')
    ftp = zoneFTP()

if __name__ == "__main__":
    main()

Calling notify() in the zoneFTP class fails. How can I make the notify() function like one of the python built in functions so that it can be called anywhere? Or is there a better way of doing what I am trying to accomplish here?


Put notify() in a utility module and have all the other modules import it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜