Home > database >  How to create a class instance within a function?
How to create a class instance within a function?

Time:08-05

My code

# Import class Parser from parser.py
from parser import Parser 

from multiprocessing.pool import Pool

def main(id):
    ... 
    url = 'test_url.com'
    Parser = Parser()
    Parser.get_response(url)
    Parser.get_beautifulsoup()
    ...

ids = [id for id in range(1, 100)]

p = Pool(20)
p.map(main, ids)

Problem

I need to call the class Parser within the main() function, but I have an error UnboundLocalError: local variable 'Parser' referenced before assignment.

Question

How can I correctly call the Parser class inside the main()?

CodePudding user response:

Hmm...don't name your instance the same as the class. Do something like:

my_Parser = Parser()
  • Related