While programming in Python, sometimes I need to launch functions and object methods, something like this:
obj1.launch_method1(); // object method
do_something(); // general function
obj1.launch_method2(); // object method
Using the with
statement, this becomes:
with obj1:
launch_method1();
do_something();
launch_method2();
In my opinion, this causes confusion, because the next programmer might erroneously think that do_something()
is an object method rather than a general function.
In top of this, most IDEs has intellisense, so when you type obj1.
(mind the dot), a list of methods and properties appears, which makes it pretty easy to type things like obj1.launch_method1()
, obj1.launch_method2()
, ...
So, from a programmer's perspective, there seems not to be an advantage in the usage of the with
statement.
However, it seems that the with
statement launches __enter__
and __exit__
calls, which seems to create new contexts. What are those calls? What does this mean? Does the usage of the with
statement make any difference? If yes, which one(s)?