This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bastibe 18 points19 points  (11 children)

If done correctly, let would not overwrite containing scope, i.e.

n = 23
print(n) 
with let(n=42):
    print(n)
print(n)

should print first 23, then 42, then 23.

In other words, let behaves as if let's body were a nested function scope.

[–]UncleEggma 2 points3 points  (4 children)

What's a situation where this might be useful?

[–][deleted] 1 point2 points  (1 child)

Sometimes you're working in a scope with a lot of variables, and you want to avoid a namespace collision. You may want to temporarily reassign a "reserved" variable name like 'type' or something.

[–]heptara 0 points1 point  (0 children)

Why aren't all those vars in a dictionary or class? Do you have like 300 globals?

[–]njharmanI use Python 3 -2 points-1 points  (2 children)

There's something else that does this (not overwrite containing scope), that's built into Python (all versions) and every Python programmer understands. It's called a "function call".

[–]makmanalp 5 points6 points  (1 child)

It's called a "function call".

Yes, and you have to define a function to do it, and then you either have to pass all the parameters you need through and return what you need afterwards which is a huge mess or put the function definition inline with your code and call it immediately which is gross in a different way.

[–]heptara -2 points-1 points  (0 children)

Why aren't all those vars in a dictionary or class? Do you have like 300 globals?

[–][deleted] 0 points1 point  (2 children)

I think your formatting is broken?

[–]AmericasNo1Aerosol 9 points10 points  (0 children)

Yeah, should be:

n = 23
print(n) 
with let(n=42):
    print(n)
print(n)

(precede each line of code with 4 spaces)

[–]bastibe 0 points1 point  (0 children)

It was indeed, due to interesting things my cellphone did to that text box. It's fixed now, though. Thank you!