For instance:

@contextmanager
def add_stuff(arg):
    parser = Parser(arg)
    # do something here
    yield parser
    # do something there
    do_stuff(parser)

Usually, I call it like this:

with add_stuff("foo") as p:
  p.add_more_stuff(1, 2, 3)
  p.add_more_stuff(4, 5, 6)

But sometimes I don't need to "add more stuff"

with add_stuff("foo"):
  pass

Is there a way to call add_stuff in one line (w/o violating pep8 by putting pass on the same line)?


Example below doesn't work because add_stuff is not just generator and is not iteratable:

list(add_stuff("foo"))

Or should I just add some util func?

def wizz(gen):
  with gen:
    pass

wizz(add_stuff("foo"))

Or keep using with...

1 Answers

0
Davis Herring On
def use(cm):
    with cm:
        pass

Then use(add_stuff("foo")) is PEP 8-y.