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,
output
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
Reason for error: The function a() is not a static method, so it needs to be instantiated before it can be used, corrected as follows.
# 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() needs to instantiate the method
Similar Posts:
- Python Error-TypeError:takes 2 positional arguments but 3 were given
- Python TypeError: file must have ‘read’ and ‘readline’ attributes
- Python: __ new__ Method and the processing of typeerror: object() takes no parameters
- [Solved] Python TypeError: sequence item 0: expected str instance, int found
- Python read CSV file prompt “line contains null byte” error
- Python: How to Solve raise JSONDecodeError(“Expecting value”, s, err.value) from None json.decoder…
- How to use htmltestrunner.py to report importerror: no module named ‘stringio’ in Python 3
- Error reporting using Yum: Yum except keyboardinterrupt, e
- How to Solve Python SyntaxError: EOL while scanning string literal
- [Solved] The paramiko module failed to upload the file: paramiko.ssh_exception.SSHException: Channel closed.