PyNEwbie
PyNEwbie

Reputation: 4940

How do I correctly create methods in a Class

I am trying to force myself to understand how to use classes. My code has gotten longer and messier. I am hoping that using classes will help me clean it up some

from lxml import html

Class Header(object):
    def __init__(self,file_reference)
        self.header =  open(file_reference).read()

    def filing_type(self):
        tree = html.fromstring(self)
        for element in tree.iter():
            if element.tag == 'type':
                return element.text.strip()

so I have a reference to a particular file

 myref = 'correct_file_path'
 test_header = Header(myref)

when I do a dir(test_header) I see my filing_type function in the list. However when I run

 test_header.filing_type()

I get a TypeError

 TypeError: 'Header' object is not subscriptable

Upvotes: 1

Views: 63

Answers (1)

zhangyangyu
zhangyangyu

Reputation: 8610

You should use tree = html.fromstring(self.header). And it seems your return in filling_type is wrong because it will only return the first tag's contents(Maybe you just want this). Maybe you could use a list to store all the type tags or use yield.

Upvotes: 3

Related Questions