使用_init_定义以在Python中初始化对象

时间:2013-04-26 08:58:22

标签: python class ini

我已经用Python编写了一个算法,现在我试图让它更加面向对象。我对对象和类有很好的理解(我认为),我花了一些时间在线阅读Python中类的语法。但是,我想我的问题非常基础,能得到一些帮助会很棒。

我创建了一个包含3个定义的类XML。我也使用__init__来初始化对象。

    class XML():

       def __init__(self,f):
          self.f = f


       def xmlToString(self):
           data = self.f.read()
           self.f.close()
           ...
           return station_arr


       def exportArray(self):
           f= open('stations/'+self.STATION+'.txt')
           lines= f.readlines()
           ...
           return phenomena,parameters


       def calcAvg(self):
           split_phenom = self.phenomena.split(';')
           list_of_lists = []
           for e in self.parameters:
              ...
           return phenomena,parameters

然后,在main.py中我实例化对象并调用我想要的方法:

       stations_names ['one', 'two'...]

       for station in stations_names:
           f = open('respond.txt','r')

           xmlStr = ClassXML.XML(f) 
           stations_arr =  xmlStr.xmlToString()


           xmlRead = ClassXML.XML(stations_arr)
           phenomena,parameters = xmlRead.exportArray()

           xmlRetr = ClassXML.XML(phenomena,parameters)
           avg_dict,dict_values = xmlRetr.calcAvg()

我得到的错误是:

f= open('stations/'+self.station+'.txt')
AttributeError: XML instance has no attribute 'station'

所以我明白这是什么问题。一些我如何将变量“站”传递给类。但是当我尝试将其包含在 init 函数中时,我会遇到不同的错误:

xmlStr = ClassXML.XML(f) 
TypeError: __init__() takes exactly 3 arguments (2 given)

然后我想也许我必须有多个 init 函数,但据我所知这在Python中是不可能的。 说实话,我真的不知道如何处理这个问题。任何提示都会有用。

由于 d

P.S。我不确定标题是否正确解释了我的问题,但我找不到任何正确的词语来表达它!

实施最终答案

    class XML():

       def __init__(self,f,station):
          self.f = f
          self.station =station


       def xmlToString(self):
           data = self.f.read()
           self.f.close()
           ...
           return station_arr


       def exportArray(self):
           f= open('stations/'+self.STATION+'.txt')
           lines= f.readlines()
           ...
           return phenomena,parameters


       def calcAvg(self,phenomena,parameters):
           split_phenom = self.phenomena.split(';')
           list_of_lists = []
           for e in self.parameters:
              ...
           return avg_dict,dict_values

**主要**:

     for station in stations_names:
         f = open('respond.txt','r')
         ## Instantiate class: ClassXmlString
         xmlStr = ClassXML.XML(f,station) 
         stations_arr =  xmlStr.xmlToString()
           if stations_arr !='':
              phenomena,parameters = xmlStr.exportArray()
              avg_dict,dict_values = xmlStr.calcAvg(phenomena,parameters)

2 个答案:

答案 0 :(得分:2)

class XML():

   def __init__(self,f,station):
      self.f = f
      self.station=station


   def xmlToString(self):
       data = self.f.read()
       self.f.close()
       ...
       self.station_arr = station_arr


   def exportArray(self):
       #here you need to use self.station_arr

       f= open('stations/'+self.station+'.txt')
       lines= f.readlines()
       ...
       self.phenomena=phenomena
       self.parameters=parameters

   def calcAvg(self,):

       #here you need to use self.phenomena and self.parameters

       split_phenom = self.phenomena.split(';')
       list_of_lists = []
       for e in self.parameters:
          ...
       self.avg_dict = avg_dict
       self.dict_values = dict_values

   def makeOutput(self):
       #call all your functions
       self.xmlToString()
       self.exportArray()
       self.scalcAvg()
       return self.avg_dict , self.dict_values


#now in your main you need to instanciate your class once! not each time you need to call a method:

stations_names ['one', 'two'...]

       for station in stations_names:
           f = open('respond.txt','r')

           xmlStr = ClassXML.XML(f,station) 
           avg_dict,dict_values =  xmlStr.makeOutput()

没试过,但应该有效。

答案 1 :(得分:1)

我认为你可以改变解决方案的组织方式,使事情变得更容易。

根据您发布的代码,我假设:

  • xmlToString将文件f和station作为参数
  • exportArray将stations_arr作为参数
  • calcAvg将(现象,参数)作为参数

我还假设您最终对(avg_dictdict_values)感兴趣。也就是说,这个解决方案的稍微重构版本是这样的:

主要代码:

   stations_names ['one', 'two'...]
   for station in stations_names:
       my_xml_object         = ClassXML.XML('respond.txt', station) 
       avg_dict, dict_values = my_xml_object.calcAvg()

类别:

class XML():

   def __init__(self, f_name, station):
      # 1 - define self.data
      with open(f_name, 'r') as f:
          self.data = f.read()
      # 2 - define self.station_arr
      self.station_arr = self.xmlToString(station)
      # 3 - Finally define (phenomena, parameters), which 
      # will be used by calcAvg()
      self.phenomena, self.parameters = self.exportArray(station_arr)

   def xmlToString(self, station):
       data = self.data
       ...
       return station_arr

   def exportArray(self, station_arr):
       # you must define self.STATION somewhere
       f = open('stations/' + self.STATION + '.txt')
       lines = f.readlines()
       ...
       return phenomena, parameters


   def calcAvg(self):
       split_phenom = self.phenomena.split(';')
       list_of_lists = []
       for e in self.parameters:
          ...
       return phenomena, parameters

我没有测试过,但最重要的是你明白了。