Modifying the fundamental tuples for doing that is certainly overkill - but maybe a context-helper function in contextlib that would proper handle all the corner cases of some code as I've pasted now at:
https://gist.github.com/jsbueno/53c059380be042e2878c08b5c10f36bf (the link above actually have working code to implement the OP sugestion as a generator-function) Not it is easier to use than contextlib.ExitStack https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack Note that for literal, hard coded tuples with known size, this is not needed at all - just spell the `with resource as bla, resource2 as ble: ` syntax works - and, for an arbitrary number of resources "tuple" hardly would be the more appropriate type to use anyway. js -><- On Fri, 12 Jul 2019 at 10:57, Anders Hovmöller <bo...@killingar.net> wrote: > You should expand a bit. How is that better than > > with open(..) as a, open(..) as b: > > > ? > > > On 12 Jul 2019, at 15:27, haael <ha...@interia.pl> wrote: > > > > > > Could we add __enter__ and __exit__ to the tuple type? > > > > Look at the following code: > > > > > > a = open('a.tmp', 'w') > > b = open('b.tmp', 'w') > > > > with (a, b) as (af, bf): > > af.write("1") > > bf.write("2") > > > > > > > > > > Even better example: > > > > > > with tuple(open(str(_n) + '.tmp', 'w') for _n in range(1000)) as f: > > for n, fn in enumerate(f): > > f.write(str(n)) > > > > > > > > > > Tuple as context manager would invoke __enter__ for each of its elements > and return a tuple of the results. > > > > On exit, the __exit__ method would be invoked for every element. > > > > > > We could even generalize it to every kind of iterable. > > > > This is somewhat consistent with treatment of exception types in > 'except' clause. > > > > > > try: > > something() > > except Exception1 as error: > > handlerA(error) > > except (Exception2, Exception3) as error: > > handlerB(error) > > > > > > > > Tuple of exception types is accepted in 'except' clause, as well as a > single exception type. We could apply that rule to the 'with' clause. > > _______________________________________________ > > Python-ideas mailing list -- python-ideas@python.org > > To unsubscribe send an email to python-ideas-le...@python.org > > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/QCYHV6FULSAIEUD2GQDG2LT6USP4N6DB/ > > Code of Conduct: http://python.org/psf/codeofconduct/ > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/YC3RJ6L7ERKEPLWKSNP2EFTAYJ2KUAOV/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/KHRXKVSHGBE3OC5E66VLHAKKNBYFNLVR/ Code of Conduct: http://python.org/psf/codeofconduct/