打印机调用方法中的未绑定方法错误消息

时间:2014-03-26 14:00:08

标签: python

这是代码的一部分;似乎给我这个问题的部分。这当然是更大计划的一部分。当我运行它时会给出错误消息:

unbound method start_data_printer() must be called with printer instance as first argument (got nothing instead)

我之前已经处理过这个错误,但这次不能弄明白。

from start_geonode_correction_new_style_class_2 import data
## import from the data class

class printer(data):

    def start_data_printer(data): ## method to print from data class
            import csv
            generator_start = data.data_out_start() ## data_out_start: a
                                                    ## data sifting method in 
                                                    ## data class
            outfile = csv.writer(open('start_data_output.csv','wb'))
            for row in generator_start:
                print row[0:]
                outfile.writerows([row])
                ## writes output from data_func_start function to a .csv file

以下是运行程序的主类调用

import os
os.chdir('C:\Users\U2970\Documents\ArcGIS')
reader = open('converted_dataset_01_13_2014_added_top_new_data_01_29_14.csv', 'rb')
from data_printer import printer

class function_calls(object):
    if __name__=='__main__':

        data_set = printer(reader)
        printer.start_data_printer() ## this is where it says the error is             

1 个答案:

答案 0 :(得分:0)

在您的代码printer中是一个类。您试图直接使用类而不是对象调用打印机方法,并且您没有将参数数据传递给该方法。

# create printer object
my_printer = printer()
# call method using object
my_printer.start_data_printer()

这将修复方法调用问题。现在方法应该定义为:

class Printer(data):

     def start_data_printer(self, generator):
         generator_start = generator.get_start()
         self.calculate_something()
         ...