zoukankan      html  css  js  c++  java
  • python遇到动态函数---TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

    TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

    # encoding: utf-8
    
    import time
    import threading
    
    class test:
    
        def follow(self,thefile):
            thefile.seek(0,2)
            while True:
                line = thefile.readline()
                if not line:
                    time.sleep(0.1)
                    continue
                    yield line
    
    if __name__ == '__main__':
    
        # obj = test()
        file1 = 'log.txt'
        file2 = 'result.txt'
        logfile = open(file2,"r")
        # loglines = obj.follow(logfile)
        loglines = test.follow(logfile)
        for line in loglines:
            print line,

    运行结果

    C:Python27python.exe C:/Users/sys_syspifarm/.ssh/MagicBox/source/test.py
    Traceback (most recent call last):
    File "C:/Users/sys_syspifarm/.ssh/MagicBox/source/test.py", line 44, in <module>
    loglines = test.follow(logfile)
    TypeError: unbound method follow() must be called with test instance as first argument (got file instance instead)

    Process finished with exit code 1

    错误原因:函数a()非静态方法,故需实例化然后才能使用,改正如下:

    # encoding: utf-8
    
    import time
    import threading
    
    class test:
    
        def follow(self,thefile):
            thefile.seek(0,2)
            while True:
                line = thefile.readline()
                if not line:
                    time.sleep(0.1)
                    continue
                    yield line
    
    if __name__ == '__main__':
    
        obj = test()
        file1 = 'log.txt'
        file2 = 'result.txt'
        logfile = open(file2,"r")
        loglines = obj.follow(logfile)
        # loglines = test.follow(logfile)
        for line in loglines:
            print line,
    

    obj = test()需要把方法实例化一下

  • 相关阅读:
    MyPHPdumpTool:MySQL 数据库备份处理方案
    sdcvx:轻量级的词典工具
    Fedora中你用GNOME还是KDE?
    Linux/GNU课程
    Fireflix:便利 Flickr 用户的 Firefox 扩展
    gtkchtheme
    recordMyDesktop:录制你的 Linux 桌面
    Fedora 8.0 NS2.33拆卸手记
    办理selinux招致无法进入零碎
    ie在Ubuntu8.04下的安装进程
  • 原文地址:https://www.cnblogs.com/anita-harbour/p/9298932.html
Copyright © 2011-2022 走看看