Python Frequently Asked Questions, Release 3.9.5 Chapter 3. Design and History FAQ 46 This also has the side-effect of increasing execution speed because name bindings are resolved at run-time in Python, and the second version only needs to perform the resolution once. Python has a ‘with’ statement that wraps the execution of a block, calling code on the entrance and exit from the block. Some languages have a construct that looks like this: In Python, such a construct would be ambiguous. Other languages, such as Object Pascal, Delphi, and C++, use static types, so it’s possible to know, in an unambiguous way, what member is being assigned to. This is the main point of static typing – the compiler always knows the scope of every variable at compile time. Python uses dynamic types. It is impossible to know in advance which attribute will be referenced at runtime. Member attributes may be added or removed from objects on the fly. This makes it impossible to know, from a simple reading, what attribute is being referenced: a local one, a global one, or a member attribute? For instance, take the following incomplete snippet: The snippet assumes that “a” must have a member attribute called “x”. However, there is nothing in Python that tells the interpreter this. What should happen if “a” is, let us say, an integer? If there is a global variable named “x”, will it be used inside the with block? As you see, the dynamic nature of Python makes such choices much harder. The primary benefit of “with” and similar language features (reduction of code volume) can, however, easily be achieved in Python by assignment. Instead of: def foo(a): with a: print(x) function(args) .mydict[index][index].a =21 function(args) .mydict[index][index].b =42 function(args) .mydict[index][index].c =63 ref =function(args) .mydict[index][index] ref.a =21 ref.b =42 ref.c =63 dir =r"	his \is\my\dos\dir "" \\ " dir =r"	his \is\my\dos\dir \ "[:-1] dir =" \\ this \\ is \\ my \\ dos \\ dir \\ " 3.25 Why doesn’t Python have a “with” statement for attribute as- signments? with obj: a =1 # equivalent to obj.a = 1 total=total+1 # obj.total = obj.total + 1 write this: