Python类型错误的原因';未绑定方法';?

Python类型错误的原因';未绑定方法';?,python,function,python-2.7,class,methods,Python,Function,Python 2.7,Class,Methods,我对Python非常陌生,如果我是一个讨厌的人,很抱歉,但是每当我尝试我的代码时,我总是收到这个错误。错误如下: Traceback (most recent call last):<br> File "<pyshell#3>", line 1, in <module> fopen.open_file() TypeError:<br> unbound method open_file() must be called with open

我对Python非常陌生,如果我是一个讨厌的人,很抱歉,但是每当我尝试我的代码时,我总是收到这个错误。错误如下:

Traceback (most recent call last):<br>
  File "<pyshell#3>", line 1, in <module>
    fopen.open_file()
TypeError:<br> unbound method open_file() must be called with openFile instance as first argument (got nothing instead)

我想你是想把file_to_open定义为openFile类的一个成员函数,对吧?如果是这样,那么您应该将“self”作为参数:

def file_to_open(self):
 ...
你应该这样称呼它:

f = openFile()
f.file_to_open()

也就是说,我认为您应该重新考虑在这里使用类。相反,您可以只定义一个函数

def open_a_file():
  filename = raw_input('Enter the file path : ')
  return open(filename)

为什么要使用类?引发的异常与您的代码不对应。您是如何使用该类创建实例的?
def open_a_file():
  filename = raw_input('Enter the file path : ')
  return open(filename)