1

我已经用 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'

所以我明白是什么问题。我必须如何将变量“station”传递给类。但是当我尝试将它包含在init函数中时,我得到了不同的错误:

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

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

感谢:D

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

实施的最终答案

    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)
4

2 回答 2

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()

没试过,但应该可以。

于 2013-04-26T09:07:36.500 回答
1

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

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

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

我还假设您最终对 ( avg_dict, dict_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

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

于 2013-04-26T09:25:43.620 回答