[Tutor] how to pass data to aother function from a class?

Kent Johnson kent37 at tds.net
Tue Feb 2 16:12:04 CET 2010


On Tue, Feb 2, 2010 at 8:04 AM, Zheng Jiekai <zjkhere at gmail.com> wrote:
> I'm beginning my python learning. My python version is 3.1
>
> I‘v never learnt OOP before.
> So I'm confused by the HTMLParser
>
> Here's the code:
>>>> from html.parser import HTMLParser
>>>> class parser(HTMLParser):
> def handle_data(self, data):
>          print(data)
>
>>>> p = parser()
>>>> page = """<html><h1>Title</h1><p>I'm a paragraph!</p></html>"""
>>>> p.feed(page)
> Title
> I'm a paragraph!
>
>
> I'm wondering if I can pass the data generated by ' handle_data' to a
> function instead of just print the data.

Sure. In fact print() is a function. Just replace print(data) with
my_function(data).

Kent


More information about the Tutor mailing list