Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force local scope in Python?

Tags:

In C++, you can do this to force local scope:

{     int i = 1;     // Do stuff } // local variable i is destroyed {     int i = 7;     // Do more stuff } 

This has the benefit that by the end of a forced local scope, any variables declared in the bracket are gone. This can help prevent using a previously defined variable x in a place later on where you didn't intend to use x.

Can you do this in Python? If so, how?

==UPDATE==

I'm aware of functions - which is the obvious thing. I was wondering if there was a quick way to do the above when the code is simple and not worth creating separate a function for - just some quick notation to emphasize that the variables in this block are not to be used anywhere else in the function.

From what people have said so far the short answer is no.

(I understand that there are clever ways like "del", or that this desire to have blocks may suggest refactoring into a separate function anyway. However I would like to emphasize this is just for short snippets where you want to emphasize the variables in this small block are not to be used elsewhere.)

like image 568
dmonopoly Avatar asked Mar 04 '14 05:03

dmonopoly


People also ask

Does Python have local scope?

Local (or function) scope is the code block or body of any Python function or lambda expression. This Python scope contains the names that you define inside the function. These names will only be visible from the code of the function.

What is local () in Python?

Definition and Usage The locals() function returns the local symbol table as a dictionary. A symbol table contains necessary information about the current program.

How do you declare a local variable in Python?

In Python or any other programming languages, the definition of local variables remains the same, which is “A variable declared inside the function is called local function”. We can access a local variable inside but not outside the function.

How do you use a local variable outside a function in Python?

If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.


1 Answers

In Python, if you declare a variable inside a function, it is local and cannot be accessed outside the function

>>> def x():     i = 5   >>> x() >>> i  Traceback (most recent call last):   File "<pyshell#5>", line 1, in <module>     i NameError: name 'i' is not defined >>>  

Alternatively, you can delete the variable from the namespace at the end so that you cannot reuse it.

>>> i = 5 >>> del i >>> i  Traceback (most recent call last):   File "<pyshell#8>", line 1, in <module>     i NameError: name 'i' is not defined >>>  
like image 195
icedtrees Avatar answered Sep 21 '22 16:09

icedtrees