0

I'm getting a problem when referencing variables on a python file. Here is the code:

FG_E = 9
FG_R = 8
START = 7
READY = 9
MC = 3
BRAKE = 5
ERROR = 6
a = 2
b = 3

position = 0

def build_message(signal):
    message = position
    message = message | (0b1<<signal)
    s = bin(message)
    s = s[2:len(s)]
    s = (16-len(s))*'0' + s

    s0 = s[0:len(s)/2]
    s1 = s[len(s)/2:len(s)]
    s0 = s0[::-1]
    s1 = s1[::-1]
    s_final = int(s0 + s1, 2)
    position = s_final
    print bin(s_final)
    return s_final

build_message(FG_R)

The error I get is: UnboundLocalError: local variable 'position' referenced berofe assigment

3
  • 1
    give global position inside function before using position variable Commented Sep 27, 2016 at 10:38
  • for more understanding see python-course.eu/python3_global_vs_local_variables.php Commented Sep 27, 2016 at 10:39
  • Please give your variables meaningful names. Commented Sep 27, 2016 at 10:46

3 Answers 3

3

The problematic line is actually position = s_final in the function build_message.

If it wasn't there then message = position would work because the Python interpreter would know to which position variable you are referring.

But in this case it is ambiguous because you're are later reassigning to position (position = s_final).

You should either re think the design of the code, or add global position as the first line in build_message. Keep in mind that as it says, it would make position a global variable and build_message will change the value of position every where throughout your code.

EDIT A quick demo:

global_var = 0

def foo1():
    print(global_var)

def foo2():
    print(global_var)
    global_var = 1

def foo3():
    global global_var
    print(global_var)
    global_var = 1
    print(global_var)

foo1()
>> 0

foo2()
>> UnboundLocalError: local variable 'global_var' referenced before assignment

foo3()
>> 0
   1
Sign up to request clarification or add additional context in comments.

4 Comments

Why Python behave like this? means message = position it would work we can not assign anything to position until it is declared as global.
@KalpeshDusane Because it creates an ambiguity. Without position being global, position = s_final can't tell if you are trying to create a new, local position variable or if you meant to assign to the global position variable.
So if we do not use global then value of position in this statement message = position is zero or not?
@KalpeshDusane It is 0, as long as there's no line that reassigns position to something else below that. See my updated answer.
1

You need to use global keyword to access global variable.

def build_message(signal):
    global position
    message = position

Comments

0

If you are using an outside variable into a function maybe you should consider passing it as an argument, like:

def build_message(signal,position):
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.