The correctness of the variable area

I am currently developing some things in Python, and I have a question about the scope of variables.

This is the code:

a = None anything = False if anything: a = 1 else: a = 2 print a # prints 2 

If I delete the first line (a = None), the code still works as before. However, in this case, I will declare the variable inside the "if" block, and with respect to other languages, such as Java, this variable will be visible only inside the "if".

How exactly does the scope variable work in Python and what is a good way to program in such cases?

Thanks!

+7
scope python
source share
5 answers

Typically, areas are created in three places:

  • File scope - otherwise known as module scope
  • Class scope - created inside class blocks
  • Area function - created inside def blocks

(There are a few exceptions to them.)

The name assignment reserves it in the area namespace marked unbound until the first assignment is reached. So, for the mental model, you assign values ​​to the names in the area.

+8
source share

I believe Python uses a scope of possibilities for local variables. That is, in any given function, if you assign a value to a local variable, it will be available from now on and within this function until it returns. Therefore, since both branches of your code are guaranteed to be assigned to a , there is no need to first assign None to a .

Note that when you can also access variables declared in external functions, in other words, Python has closures.

 def adder(first): def add(second): return first + second return add 

This defines a function called an adder. When called with the first argument, it returns a function that adds any argument that it receives to first and returns that value. For example:

 add_two = adder(2) add_three = adder(3) add_two(4) # = 6 add_three(4) # = 7 

However, although you can read the value from an external function, you cannot change it (unlike many other languages). For example, imagine trying to implement a drive. You can write code like this:

 def accumulator(): total = 0 def add(number): total += number return total return add 

Unfortunately, when trying to use this code, an error message appears:

 UnboundLocalError: local variable 'total' referenced before assignment 

This is because the line total += number tries to change the value of total , which cannot be done this way in Python.

+2
source share

There is no problem assigning a variable in the if block.

In this case, it is assigned on both branches, so you can see that it will definitely be determined when you come to print it.

If one of the branches was not assigned to a , then a NameError exception would be a raise when trying to print it after this branch

+1
source share

Python does not require variables that must be declared initially, so you can declare and define at arbitrary points. And yes, the scope is the scope, so it will be visible outside the if .

0
source share

I am a pretty novice programmer, but for what I know in python private variables do not exist. see private variables in python documentation for a detailed discussion.

useful information can also be found in the "Areas and Namespaces" section on the same page.

personally, I write code similar to the one you posted almost every day, especially when the condition depends on user input from user, for example

 if len(sys.argv)==2: f = open(sys.argv[1], 'r') else: print ('provide input file') 

I declare variables before using them for structured types, for example, I declare an empty list before adding its elements to the loop.

hope this helps.

0
source share

All Articles