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:\Python27\python.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()需要把方法实例化一下

python遇到动态函数---TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)的更多相关文章

  1. TypeError: unbound method

    调用类报错,具体如下 TypeError: unbound method submit() must be called with jinjin instance as first argument ...

  2. [Python] Python 之 function, unbound method 和 bound method

    首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...

  3. python学习之函数

    1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回N ...

  4. Bound Method and Unbound Method - 绑定方法 与 非绑定方法

    Bound Method and Unbound Method 通常有两种方法对类的方法(instance.method)/属性(class.attribute)进行引用, 一种称做 Bound Me ...

  5. [Python] First-class Everything (Python缔造者Guido van Rossum关于bound/unbound method的来历叙述)

    First-class Everything -- Guido van Rossum First-class object: 第一类对象.意指可在执行期创建并作为参数传递给其他函数或存入一个变量的对象 ...

  6. 给python类动态添加方法(method)

    群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...

  7. python动态函数名的研究

    所谓动态函数名,就是使用时完全不知道是叫什么名字,可以由用户输入那种. 一般人习惯性会想到eval或exec, 但是众所周知,这样的写法不安全而且容易引起问题,而且不pythonic.而且使用时必须把 ...

  8. Python基础三. 函数、lambda、filter、map、reduce

    一.概述 函数, 就是用一些语句组织起来实现一组特定的功能, 用来重复调用. 函数的作用及意义:最大化的重用代码和最小化的代码冗余以及对流程的分解. Python中有哪些函数: 内建的函数 第三方模块 ...

  9. 由Python的super()函数想到的

    python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...

随机推荐

  1. windows系统常用命令

    dir 指定要列出的驱动器,显示当前文件夹下的文件   /?可显示所有命令 显示当前路径下的所有文件的绝对路径,包含子文件夹中的内容 D:\test > dir /b /s /o:n /a:a  ...

  2. 一致性hash算法应用场景、详解与实现(JAVA)

    一.概述 在分布式环境下,开发者通常会遇到一些分布存储的场景,例如数据库的分库分表(比如用户id尾号为1的放入数据库1,id尾号为2的放入数据库2):又如分布式缓存数据的获取(比如根据ip地址进行缓存 ...

  3. 长乐国庆集训Day5

    T1 方阵 题目 [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑是有一定规律的.兵马俑阵总共有n ...

  4. LeetCode第154场周赛(Java)

    估计要刷很久才能突破三道题了.还是刷的太少.尽管对了前两题,但是我觉得写的不怎么样.还是将所有题目都写一下吧. 5189. "气球" 的最大数量 题目比较简单.就是找出一个字符串中 ...

  5. IDEA 开发插件

    Alibaba Java Code Guidelines 阿里巴巴推出的一款Java代码规约扫描插件,按照<阿里巴巴Java开发手册>规定对代码风格以及质量进行实时检测.约束.强推.ecl ...

  6. centos 7安装jdk并封装service服务

    一.概述 有一个Spring Cloud的jar包,文件名为:RDS.jar.必须要jdk1.8版本,需要部署在 Centos 7.5的服务器上面,最好能设置开机自启动! 二.安装jdk 关闭防火墙 ...

  7. Jenkins版本迭代以及回滚

    一.摘要 在上一篇文章,链接如下: https://www.cnblogs.com/xiao987334176/p/11434849.html 镜像打的是latest版,如果需要回滚的话,就比较麻烦了 ...

  8. JVM 参数调优配置

    在 tomcat 配置文件 tomcat/bin/catalina.sh 中 配置  JAVA_OPTS="-server -Xms2048m -Xmx2048m -Xss1024K -XX ...

  9. js提取135编辑器相同的css

    135编辑器导入的内容,有重复的很多css,导致加载很慢,只能去掉 function remove135FormatContent(content){ if(!content) return ''; ...

  10. selenium中的元素操作之下拉列表操作(四)

    下拉列表操作中分为两种:select.非select 1.非select的下拉框操作 非select下拉列表操作与网页元素操作一致,找到元素,定位元素,设置等待,点击元素等等 接下来操作百度的设置按钮 ...