Note that there are some explanatory texts on larger screens.

plurals
  1. POUse the _init_ definition in order to initialize an object in Python
    primarykey
    data
    text
    <p>I have written an algorithm in Python and now I am trying to make it a bit more object oriented. I have a good understanding (I think) of objects and classes and I have spend some time reading online the syntax for classes in Python. However, I guess my question is quite basic and it would be great to get some help.</p> <p>I have created a Class XML which contains 3 definitions. I also have used <code>__init__</code> to initialize the object.</p> <pre><code> 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 </code></pre> <p>Then, in the main.py I instantiate the objects and call the methods I want like this:</p> <pre><code> 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() </code></pre> <p>The error I get is this:</p> <pre><code>f= open('stations/'+self.station+'.txt') AttributeError: XML instance has no attribute 'station' </code></pre> <p>So I understand what is the problem. Some how I have to pass into the class the variable "station". But when I try to included it in the <strong>init</strong> function I get different errors:</p> <pre><code>xmlStr = ClassXML.XML(f) TypeError: __init__() takes exactly 3 arguments (2 given) </code></pre> <p>Then I thought maybe I have to have multiple <strong>init</strong> functions but as far as I know this is not possible in Python. To be honest I don't really know how to handle the problem. Any tip would be useful. </p> <p>Thanks D</p> <p>P.s. I am not sure if the title explains correctly my question, but I can not find any correct words to put it!</p> <p><strong>IMPLEMENTED FINAL ANSWER</strong></p> <pre><code> 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 </code></pre> <p>** Main **:</p> <pre><code> 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload