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 ...
随机推荐
- [转] spring-boot集成swagger2
经测,spring-boot版本使用1.5.2+时需使用springfox-swagger2版本2.5+(spring-boot 1.2 + springfox-swagger2 2.2 在未扫描ja ...
- Eclipse项目上传和下载到码云上
本文将介绍如何将本地的项目提交到开源中国的码云(版本控制器)上.改教程讲解过程比较详细,跟着做实现起来很简单.由于自己本身也是一个新手,所以不做过多的解释,只是单纯的描述了该如何去做,大家一起学习共同 ...
- Google Colab——零成本玩转深度学习
前言 最近在学深度学习HyperLPR项目时,由于一直没有比较合适的设备训练深度学习的模型,所以在网上想找到提供模型训练,经过一段时间的搜索,最终发现了一个谷歌的产品--Google Colabora ...
- C++实现base64编解码
Base64是常见的加密算法,代码实现了基于C++的对于base64的编码和解码. 其中注释掉的部分为编码部分,取消注释将解码部分注释掉即可实现编码,反之可以实现解码. #include <st ...
- collections模块之defaultdict()与namedtuple()方法简单介绍
一.defaultdict() 作用:根据数据创建字典时,需要为一些数据生成字典,而且对值得类型进行限定的时候,考虑defaultdict from collections import defaul ...
- Python之路【第二十九篇】:django ORM模型层
ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...
- GoLang 的变量
变量 1.为什么要变量 1.1.一个程序就是一个世界 1.2.变量是程序的基本组成单位 2.变量的介绍 2.1.变量的概念 变量相当于内存中一个数据存储空间的表示,你可以把变量看做是一个房间的门牌号, ...
- 【leetcode-11】盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- 移动端开发rem布局之less+媒体查询布局的原理步骤和心得
rem即是以html文件中font-size的大小的倍数rem布局的原理:通过媒体查询设置不同屏幕宽度下的html的font-size大小,然后在css布局时用rem单位取代px,从而实现页面元素大小 ...
- Spring MVC之@ControllerAdvice详解
本文链接:https://blog.csdn.net/zxfryp909012366/article/details/82955259 对于@ControllerAdvice,我们比较熟知的用法是 ...