views:

311

answers:

8

In many languages (and places) there is a nice practice of creating local scopes by creating a block like this.

void foo()
{
     ... Do some stuff ...

     if(TRUE)
     {
         char a;
         int b;

         ... Do some more stuff ...
     }

     ... Do even more stuff ...
 }

How can I implement this in python without getting the unexpected indent error and without using some sort of if True: tricks

+3  A: 

Why do you want to create new scopes in python anyway?

The normal reason for doing it in other languages is variable scoping, but that doesn't happen in python.

if True:
    a = 10

print a
Douglas Leeder
Variable Scoping in other languages really means memory management. Something you don't need with a garbage collector.
S.Lott
Last I checked, CPython's garbage collector doesn't DECREF until the function returns.
joeforker
A: 

variables in list comprehension (Python 3+) and generators are local:

>>> i = 0
>>> [i+1 for i in range(10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> i
0

but why exactly do you need this?

SilentGhost
@SilentGhost, run your same example with i = "SilentGhost"; [i+1 for i in range(10)]; print i. You will see that the list comprehension does indeed leak its variable.
joeforker
I see that it doesn't. py3k here.
SilentGhost
I've added (Python 3+).
J.F. Sebastian
+1  A: 

I would see this as a clear sign that it's time to create a new function and refactor the code. I can see no reason to create a new scope like that. Any reason in mind?

Jonas
A: 
def a():
    def b():
        pass
    b()

If I just want some extra indentation or am debugging, I'll use if True:

joeforker
if True doesn't work
SilentGhost
@SilentGhost it doesn't create a scope but it does visually set apart a block of code, which is why I sometimes use {} in C.
joeforker
+4  A: 

In Python, scoping is of three types : global, local and class. You can create specialized 'scope' dictionaries to pass to exec / eval(). In addition you can use nested scopes (defining a function within another). I found these to be sufficient in all my code.

As Douglas Leeder said already, the main reason to use it in other languages is variable scoping and that doesn't really happen in Python. In addition, Python is the most readable language I have ever used. It would go against the grain of readability to do something like if true tricks (Which you say you want to avoid). In that case, I think the best bet is to refactor your code into multiple functions, or use a single scope. I think that the available scopes in Python are sufficient to cover every eventuality, so local scoping shouldn't really be necessary.

I hope this helps.

batbrat
Thank you for the detailed answer
bgbg
My pleasure. Glad I could help out
batbrat
+1  A: 

If you just want to create temp variables and let them be garbage collected right after using them, you can use

del varname

when you don't want them anymore.

If its just for aesthetics, you could use comments or extra newlines, no extra indentation, though.

XenF
A: 

A scope is a textual region of a Python program where a namespace is directly accessible. “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace...

Please, read the documentation and clarify your question.

btw, you don't need if(TRUE){} in C, a simple {} is sufficient.

J.F. Sebastian
A: 

Python has exactly two scopes, local and global. Variables that are used in a function are in local scope no matter what indentation level they were created at. Calling a nested function will have the effect that you're looking for.

def foo():
  a = 1

  def bar():
    b = 2
    print a, b #will print "1 2"

  bar()

Still like everyone else, I have to ask you why you want to create a limited scope inside a function.

David Locke