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. netty中的channelPipeline在编程中的作用

    在netty编程中我们绝大多数是要是用nio的,nio相比传统的io更加高效,而nio中核心概念离不开channel,buffer,selector三个重要的对象. 那么在netty中有一个chann ...

  2. MyBatis参数条件查询传入的值为0时的判断

    MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...

  3. Word 插入页码 -- 视频教程(7)

    1. 以本科做的一个课程设计为例 >> 视频教程链接:B站,速度快,清晰 未完 ...... 点击访问原文(进入后根据右侧标签,快速定位到本文)

  4. 玩机之Honor_V10

    作为一个热爱手机的Geek,自然是经历了很多的刷机和改装手机的经验,当然翻车的经验也是有的.一般来说的折腾手机都是在遇到某一版本使用以及各方面都比较稳定的时候才会选择让手机停留在哪一版本.下面我就来分 ...

  5. postgresql学习之安装篇

    ---恢复内容开始--- 安装方法: 1.可以使用操作系统自带的安装源 2.可以使用官网下载的源码进行安装 3.可以使用编译好的包入.run格式的安装包安装(本文使用的是这种安装方法,下载地址http ...

  6. 仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢?

    原文:仅反射加载(ReflectionOnlyLoadFrom)的 .NET 程序集,如何反射获取它的 Attribute 元数据呢? 平时我们获取一个程序集或者类型的 Attribute 是非常轻松 ...

  7. ashx 接受 post json 请求

    HttpContext.Current.Response.ContentType = "application/json";            HttpContext.Curr ...

  8. MongoDB和Java(2):普通用户启动mongod进程

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  9. Spring源码——IOC控制反转

    1.基础知识 Spring有两个核心功能,分别是ioc和aop,其中ioc是控制反转,aop是切面编程. 在ioc中,还有一个名次叫DI,也就是依赖注入.嗯,好像IOC和DI是指同一个,好像又感觉他俩 ...

  10. Java中new和Class.forName的区别

    首先:New = Class.forName("pacage.A").newInstance(); new是关键字,直接创建对象.Class.forName()是一个方法,要求JV ...