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:\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)的更多相关文章
- TypeError: unbound method
调用类报错,具体如下 TypeError: unbound method submit() must be called with jinjin instance as first argument ...
- [Python] Python 之 function, unbound method 和 bound method
首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...
- python学习之函数
1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回N ...
- Bound Method and Unbound Method - 绑定方法 与 非绑定方法
Bound Method and Unbound Method 通常有两种方法对类的方法(instance.method)/属性(class.attribute)进行引用, 一种称做 Bound Me ...
- [Python] First-class Everything (Python缔造者Guido van Rossum关于bound/unbound method的来历叙述)
First-class Everything -- Guido van Rossum First-class object: 第一类对象.意指可在执行期创建并作为参数传递给其他函数或存入一个变量的对象 ...
- 给python类动态添加方法(method)
群里有人问如何做到 def foo(): pass class Bar(object): pass Bar.set_instance_method(foo) b = Bar() b.foo() 这个其 ...
- python动态函数名的研究
所谓动态函数名,就是使用时完全不知道是叫什么名字,可以由用户输入那种. 一般人习惯性会想到eval或exec, 但是众所周知,这样的写法不安全而且容易引起问题,而且不pythonic.而且使用时必须把 ...
- Python基础三. 函数、lambda、filter、map、reduce
一.概述 函数, 就是用一些语句组织起来实现一组特定的功能, 用来重复调用. 函数的作用及意义:最大化的重用代码和最小化的代码冗余以及对流程的分解. Python中有哪些函数: 内建的函数 第三方模块 ...
- 由Python的super()函数想到的
python-super *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...
随机推荐
- Openresty 健康检查
## 指定共享内存 lua_shared_dict healthcheck 1m; ## 在worker初始化过程中,启动定时器,进行后端结点的检查 init_worker_by_lua_block ...
- GitHub: Oracle RAC Database on Docker 未测试 改天试试
https://github.com/oracle/docker-images/blob/master/OracleDatabase/RAC/OracleRealApplicationClusters ...
- 一定要记住的14个JVM内存配置参数
jvm setting的参数确实比较多(Oracle官网Java HotSpot VM Options),但是作为一名java开发者,那几个最常用最基本的参数设置和意义一定要死记和理解.这里推荐一个网 ...
- OpenLayers加载谷歌地球离线瓦片地图
本文使用OpenLayers最新版本V5.3.0演示:如何使用OpenLayer加载谷歌地球离线瓦片地图.OpenLayers 5.3.0下载地址为:https://github.com/openla ...
- composer安装FOSUserBundle内存溢出
内存溢出异常: Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 4096 bytes ...
- Java的含义
Java是一种广泛使用的计算机编程语言,拥有跨平台.面向对象.泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发. Java语言它不是软件,这里给各位初学者们详细解释一下.简单来说计算机语言 ...
- 网格搜索与K近邻中更多的超参数
目录 网格搜索与K近邻中更多的超参数 一.knn网格搜索超参寻优 二.更多距离的定义 1.向量空间余弦相似度 2.调整余弦相似度 3.皮尔森相关系数 4.杰卡德相似系数 网格搜索与K近邻中更多的超参数 ...
- arc079
D. Decrease (Contestant ver.) 大意: 每次操作选一个最大数$-n$,其余数全$+1$. 要求构造一个序列$a$, 使得恰好$k$次操作后最大值不超过$n-1$. 只要让$ ...
- 在Centos中安装.net core SDK
在Linux中运行.net core 项目必须要有.net core SDK 环境.之前配置过几次,但由于没有做总结.过了几天又配置的时候 感觉特别陌生,今天就记录一次.net core SDK 的安 ...
- 自学Python编程的第五天(希望有IT大牛帮我看最下面的代码)----------来自苦逼的转行人
2019-09-15-15:40:24 今天没有学知识,是一个一周总结,把这一周学的知识总结一遍,然后把做过的练习题再做一遍 看是否还会有再出现同样的错误,而且还可以知道有哪些知识点没有掌握好,可以把 ...