Find the answer to your Linux question:
Results 1 to 2 of 2
I just started learning python a few days ago. I'm noticing some behavior that I don't understand in one of my scripts: Code: #!/usr/bin/python import main # this is main ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    5

    Smile Interesting python behavior : newb here

    I just started learning python a few days ago. I'm noticing some behavior that I don't understand in one of my scripts:

    Code:
    #!/usr/bin/python
    
    import main # this is main
    
    class ReadFile():
    	result = []
    	
    	def make_moldata(self):
    		data = open('fixed.txt','r')
    		universe = data.readlines()
    		data.close()
    		datum = []
    		target = float()
    		for line in universe:
    			if line == '\n':
    				self.result.append(main.MolData(targ=datum[0], descs=datum[1:-1]))
    				datum = []
    			else:
    				datum.append(line.strip())
    		return self.result
    		
    class MolData():
    	descriptors = []
    	target = float()
    	
    	def __init__(self, targ, descs):
    		self.descriptors = list(descs)
    		self.target = float(targ)
    	
    	def __getitem__(self, index):
    		return self.descriptors[index]
    		
    	def __setitem__(self, index, value):
    		self.descriptors[index] = value
    	
    	def __len__(self):
    		return len(self.descriptors)
    	
    	def __repr__(self):
    		s = self.descriptors.__repr__() + ' : ' + self.target.__repr__()
    		return s
    	
    	def gettarget(self):
    		return self.target
    		
    	def get(self,index):
    		return self.__getitem__(index)
    	
    class OrderedData():
    	unordered = []
    	ordered = []
    	
    	def __init__(self,data=[]):
    		self.unordered = data
    		
    	def order(self):
    		self.ordered = self.do_it()
    		return self.ordered
    		
    	def do_it(self):           # incomplete
    		pass
    
    def run():
    	print "Running!"
    
    	universe = main.ReadFile().make_moldata()
    	ordered = main.OrderedData(universe)
    	ordered.order()
    
    	print "Bye!"
    	
    run()
    When I run ./main.py I get
    Code:
    Running!
    Bye!
    Running!
    Bye!
    but importing main in an interactive session only runs it once. Does anyone know why this is happening? Also, any critiques? Am I doing anything that screams my pythonic newbness? Suggestions for improvement?
    Thanks.

  2. #2
    Just Joined!
    Join Date
    Jan 2008
    Posts
    5

    Lightbulb Solved

    In case someone else runs into a similar problem, I found my error: it was the `import main` statement. It was causing the module to import and run itself before getting to the `run()` statement at the bottom. I had added it because I was having problems getting a module to run various functions implemented in it, but since the definition of `run()` is above it's call, in this case it works out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...