这是我的类文件(名为myclasses.py):
class wave:
def __init__(self, name = "", xdelta = 1)
self.name = name
self.xdelta = xdelta
def loadbinary(bpath):
print(bpath)
现在我已经删除了其他细节(加载二进制文件代码等),当我运行以下内容时,我收到错误输出:
import myclasses
a = myclasses.wave
a.loadbinary('TEST')
错误:
TypeError: loadbinary() missing 1 required positional argument: 'bpath'
我已经尝试将loadbinary定义更改为“loadbinary(self,bpath)”但是它会给出相同的错误。我也尝试用“pass”替换“print”代码,让它什么都不做,但这仍然给我错误。我也试过运行以下内容:
a.loadbinary(bpath='TEST')
...但是这会将错误更改为:loadbinary()缺少1个必需的位置参数:'self'
此时,我不确定发生了什么。类定义是基本的,并且不从其他类继承,并且我可以告诉我们遵循python文档中针对此处显示的类的代码:https://docs.python.org/2/tutorial/classes.html
我正在使用Python 3.4.3,这发生在iPython 3.1.0中,在PyCharm 4.5.1中运行。如果我在命令提示符(OS X终端)直接在iPython 3.1.0中运行它,我会得到以下错误输出:
unbound method loadbinary() must be called with wave instance as first argument (got str instance instead)
答案 0 :(得分:4)
你从未实例化过这门课程。这样做:
class wave:
def __init__(self, name = "", xdelta = 1)
self.name = name
self.xdelta = delta
def loadbinary(self, bpath):
print(bpath)
或者...
class wave:
def __init__(self, name = "", xdelta = 1)
self.name = name
self.xdelta = delta
@staticmethod
def loadbinary(bpath):
print(bpath)
则...
import myclasses
a = myclasses.wave() # the brackets instantiate the class
a.loadbinary('TEST')