-2

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)?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 3
    The next programmer will only erroneously think that `do_something()` is an object method if he is confusing a Python `with` statement and a Pascal `with` statement. `with obj1: launch_method()` is *not* the same as `obj1.launch_method()`. Take a look at the documentation for *context managers*. And drop the semicolons; that's another Pascal habit. – BoarGules Apr 02 '19 at 11:30
  • 3
    I downvoted because the question seems to be describing the `with` statement from a completely different language. This code example given would not behave the way it is described in Python. The excessive semicolons make me suspect they meant to ask the question about Javascript. Also the comments in the first code block aren't Python syntax either. – Duncan Apr 02 '19 at 11:33
  • 3
    I'm voting to close this question as off-topic because although it claims to be about Python it appears to be about Javascript. – Duncan Apr 02 '19 at 11:35
  • Looking at a standard `with open("file") as f:`would have shown, that no identifier can be dropped. _enter_ and _exit_ create and destruct the only context in scope, so which one should be new? – guidot Apr 02 '19 at 11:42

1 Answers1

3

The with statement is a context manager. This takes care of building and breaking down resources.

For example, if you need to use a file, traditionally you have to open the file and remember to close it afterwards. The with open statement takes care of this for you.

with open(“file.txt”) as file:  
    data = file.read() #Context of the file open

The enter method opens the file exit closes it.

Now this might seem like a lot of effort for small gain but consider a system where there is a lot of building and breaking down. You can easily build your own context manager.

For example opening a socket, Handshake, Authenticating and Authorization can all go into the enter method and breaking it all down again into the Exit method

Then it is as simple as using the context when you need it

Nhovha
  • 66
  • 5