Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: defining new functions on the fly using "with"

I want to convert the following code:

...
urls = [many urls]
links = []
funcs = []
for url in urls:
   func = getFunc(url, links)
   funcs.append(func)
...

def getFunc(url, links):
   def func():
      page = open(url)
      link = searchForLink(page)
      links.append(link)
   return func

into the much more convenient code:

urls = [many urls]
links = []
funcs = []
for url in urls:
   <STATEMENT>(funcs):
        page = open(url)
        link = searchForLink(page)
        links.append(link)

I was hoping to do this with the with statement. As I commented bellow, I was hoping to achieve:

def __enter__():
    def func():

..code in the for loop..

def __exit__():
  funcs.append(func)

Of course this doesn't work.

List comprehensions is not good for cases were the action searchForLink is not just one function but many functions. It would turn into an extremely unreadable code. For example even this would be problematic with list comprehensions:

for url in urls:
  page = open(url)
  link1 = searchForLink(page)
  link2 = searchForLink(page)
  actionOnLink(link1)
  actionOnLink(link2)
  .... many more of these actions...
  links.append(link1)
like image 463
Guy Avatar asked Dec 04 '22 13:12

Guy


1 Answers

It makes no sense to use with here. Instead use a list comprehension:

funcs = [getFunc(url, links) for url in urls]
like image 64
Ignacio Vazquez-Abrams Avatar answered Dec 25 '22 17:12

Ignacio Vazquez-Abrams