0

I've managed to do almost no OOP since I've been using python and I'm trying to get familiar with writing classes and so on.

How can I break the following class into one other function. I'd like to rather than pass two arguments to the class pass the fname first and then call a method called display for all of the xml stuff that I want to print. When I attempted to do that I ended up with Global name issues with tree.

import sys
class Parser(object):
 def __init__(self, file_name, mylist):
 import xml.etree.ElementTree as etree
 self.file_name = file_name
 tree = etree.parse(self.file_name)
 for node in tree.getiterator():
 for element in mylist:
 if element in node.tag:
 print(node.tag)
list2 = ['KeyId','X509SerialNumber','CompositionPlaylistId']
fname = sys.argv[1]
myfileinstance =Parser(fname,list2)
asked Jul 9, 2013 at 21:42
4
  • 3
    Why are you importing things inside __init__? Unless there's a very good reason for doing something else, all your imports should be at the top of the file. Commented Jul 9, 2013 at 21:44
  • 2
    What does "I ended up with Global name issues with tree" mean? And what exactly does "I attempted to do that" mean? Please show us the actual code you tried, and exactly what happened, or we can't possibly help you debug it. Commented Jul 9, 2013 at 21:45
  • Easy enough. Have the body of __init__() be just self.file_name = file_name and create a new method whose body is the rest of that code. Commented Jul 9, 2013 at 21:45
  • Yeah, I figured importing within the class was a terrible idea, as opposed to being at the top of the file. Thanks Commented Jul 9, 2013 at 21:55

1 Answer 1

1

Just make a new method to do what you want that method to do (of course I'm kind of guessing, since you haven't actually shown us the code that is more like what you wanted...). Your __init__ method should only contain logic that is necessary for creation of the object--usually this means assigning instance variables and so on.

import sys
import xml.etree.ElementTree as etree # All imports at the top of the file!
class Parser(object):
 def __init__(self, file_name):
 self.file_name = file_name
 def display(self, rename_this_variable):
 tree = etree.parse(self.file_name)
 for node in tree.getiterator():
 for element in rename_this_variable:
 if element in node.tag:
 print(node.tag)
list2 = ['KeyId','X509SerialNumber','CompositionPlaylistId']
# Worth a mention: rename this to something that's actually meaningful!
fname = sys.argv[1]
instance = Parser(fname)
instance.display(list2)
answered Jul 9, 2013 at 21:47
Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what I wanted to do!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.