Python - UnboundLocalError: local variable "ID" referenced before assignment - python

Id, conf = recognizer.predict(gray[y:y+h,x:x+w]
def hour(cn):
for z in range(9,17):
if now.hour == z:
worksheet(cn, str(z)+":00")
def identify(number):
sht = gc.open("Test")
wks3 = sht.worksheet("NAMES")
b = wks3.acell('B'+str(number)).value
a = wks3.acell('A'+str(number)).value
if(Id == a and conf<65):
print(Id, conf)
Id = str(b)
Time = time.ctime()
hour(number)
elif(conf>64):
print(conf)
Id = "Unknown"
for m in range(2,100):
identify(m)
The above code is being used for facial recognition, I copied what I felt was necessary, it is not the entire code.
I'm trying create a function which I want to call back in a for loop
What am I doing wrong? I've been looking t this for 6 hours now, and anything I try doesn't seem to work.
I get a message back saying "UnboundLocalError: local variable 'Id' referenced before assignment"
It's impossible because I'm assigning with:
a = wks3.acell('A'+str(number)).value
So it grabs the ID number from the google spread sheet and checks if it is equaled to that, can someone tell me where I'm going wrong here?

def identify(number):
sht = gc.open("Test")
wks3 = sht.worksheet("NAMES")
b = wks3.acell('B'+str(number)).value
a = wks3.acell('A'+str(number)).value
#because you did, Id = ?
if(Id == a and conf<65):
print(Id, conf)
Id = str(b)
Time = time.ctime()
hour(number)
elif(conf>64):
print(conf)
Id = "Unknown"
Because you did, variable Id isn't passed as any parameter or global/local variable or as an argument to existing class.
If Id was parameter:
def identify(number,Id):
If Id was global variable:
def identify(number):
global Id
If Id was local variable:
def identify(number):
id = None # or some other data type
And if Id was argument from some class:
some_class.Id
In short you referenced Id before it was initialised. This is rookie mistake and there is some stuff where you can actually init a variable in if elif else statement but you need to trow a none of above logic of the rule.
if True: Id = 2; elif False: Id = 3; else: Id =0 #this is pseudocode, don't paste it in.
Also have in mind that next variable is also Unbound conf
EDIT:
Often to avoid this problem we write code like this:
def somefunction(parm1,parm2... ):
# global variables : description for variable stack is optional
global var1,var2 # if needed
#local variables
var3,var4 = None;
var5 = 'something else'
#in body functions : functions inside functions or just general program functions
def a(... ): return ...
#body : actually what function/program does.
# returning , finishing statement.

Related

python variable not defined within function

I need some help with python variables. I have a sketch that sends variables to a function called roomControl. If during this function the status has changed from a previous time then I want it to do something. At present it should printout the variable values.
I am getting the error that the status variable has not been assigned and I believe this is due to it being set outside the function. But if I set the variable within the function then this will override the functions control of these variables. (hope this makes sense)
My script is below but basicly if status changes then print out something is what I am trying to achieve
import MySQLdb
status = 0
previousStatus = 0
connSolar = MySQLdb.connect("localhost", "######", "#######", "######")
cursSolar = connSolar.cursor()
def roomControl(name, demand, thermostat, roomTemp):
if demand == 1 and thermostat > roomTemp:
status = 1
if demand == 1 and thermostat < roomTemp:
status = 0
if demand == 0:
status = 0
if (status == 1 and previousStatus == 0):
print("Room: %s, Heating demand = %s, Thermostate = %s, Room Temp = %s, Status = %s") % (name, demand, thermostat, roomTemp, status)
print("")
previousStatus = status
return (status)
while 1:
Ntemp = 25
try:
cursSolar.execute ("SELECT * FROM roomHeatingControl")
connSolar.commit()
for reading in cursSolar.fetchall():
Nthermostat = reading[6]
NSW = reading[7]
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e)
NPy_status = roomControl('Niamh', NSW, Nthermostat, Ntemp)
This is related global, local variable types. You can find here https://www.programiz.com/python-programming/global-keyword explanation.
You need only define "status" as a global.
A solution is to bring in status as a global.
status = 0
previousStatus = 0
# other code
def roomControl(name, demand, thermostat, roomTemp):
global status
global previousStatus
# This will bring in both variables to be edited within the function
Good luck!
You should learn to cut out bits of your code and simplify it without losing the error. That way you will nail down the error.
Ultimately you will end up with code like this:
status = 0
def f():
print(status)
f()
Out: 0
As expected, the function can't find status, so it looks in the global scope, finds it, and prints it.
status = 0
def f():
status = 1
print(status)
f()
Out: 1
Again as expected. We've defined a local variable status for f, so f just uses that when it prints.
status = 0
def f():
print(status)
status = 1
f()
UnboundLocalError: local variable 'status' referenced before assignment
Now it becomes clear why there is an error. The only difference from the second example is that we've swapped the order so that status is defined in f only after it is used in print, and the only difference from the first example is that we define status inside f. So this is the problem: when we define a variable inside a function -- anywhere inside a function -- Python decides that that variable must be local to the function. So when it hits the print function, it looks for a local variable status, but it hasn't been defined yet. Hence the error. Similar to if you ran this code:
print(status)
status = 1
NameError: name 'status' is not defined
Avoid implementing a solution where you need to access the Global variables directly. One way is to change to a better data structure where possible.
You can define your statuses as a list or dict (I'd prefer dict) and you can access them directly inside the function without passing them. Here is the small example
to make things bit more clear
d={'status':0, 'prev_status':0} # Intialize the dict
def myfunc():
d['status']=5 # make modifications
myfunc()
print(d) # {'status': 5, 'prev_status': 0}

In Python 2.7, how can I return calculations without defining variables in the constructor?

My question is about getter/setter-type functionality in Python. I have a class, Week_Of_Meetings, that takes a blob of data from my Google Calendar and does some calculations on it.
wom = Week_Of_Meetings(google_meetings_blob)
I want to be able to return something like:
wom.total_seconds_in_meetings() # returns 36000
But, I'm not understanding how the getters/setters-type #property decorator can help me do this. In Java, I would use member variables, but you don't interact with them the same way in Python. How can I return calculations without starting with them in the constructor?
Class Week_Of_Meetings:
def __init__(self, google_meetings_blob)
self.google_meetings_blob = google_meetings_blob
def get_meetings_list(self, google_meetings_blob):
meetings_list = []
for meeting_id, meeting in enumerate(self.google_meetings_blob, 1):
summary = self._get_summary(meeting)
start = parse(meeting['start'].get('dateTime', meeting['start'].get('date')))
end = parse(meeting['end'].get('dateTime', meeting['end'].get('date')))
duration = end - start
num_attendees = self._get_num_attendees(meeting.get('attendees'))
m = Meeting(meeting_id, summary, start, end, duration, num_attendees)
meetings_list.append(m)
return meetings_list
def _get_summary(self, meeting):
summary = meeting.get('summary', 'No summary given')
return summary
def _get_num_attendees(self, num_attendees):
if num_attendees == None:
num_attendees = 1 # if invited only self to meeting
else:
num_attendees = len(num_attendees)
return num_attendees
When I add self.total_seconds_in_meetings to the
__init__()
I get "NameError: global name 'total_seconds_in_meetings' is not defined." That makes sense. It hasn't been defined. But I can't define it when it's supposed to be the result of calculations done on the google_meetings_blob. So, I'm confused where the 'total_seconds_in_meetings' goes in the class.
Thank you for the help!
Of course Python has member variables. How would classes work without them? You can set and get any instance data via self, as you are already doing with self.google_meetings_blob in __init__.

Python alter external variable from within function

When I run this it works, but it says
"name 'select_place' is assigned to before global declaration"
When I get rid of the second global, no comment appears, but as select_place is no longer global it is not readable (if selected) in my last line of code.
I'm really new to python, ideally I'd like a way of not using the global command but after searching i still can't find anything that helps.
My code:
def attempt(x):
if location =='a':
global select_place
select_place = 0
if location =='b'
global select_place
select_place = 1
place = ([a,b,c,d])
This is the start of some turtle graphics
def Draw_piece_a(Top_right):
goto(place[select_place])
You need to declare the variable first, additionally the function code can be made clearer:
select_place = False
def attempt(x):
global select_place
if location == 'a':
select_place = 0
elif location == 'b':
select_place = 1
Also, there is no return value for attempt(), is this what you want?

Unexpected change of list values

This is my class:
class variable(object):
def __init__(self, name, name_alias, parents,values,table):
#name of the variable
self.name = ""
This is the function with problems:
f is a .txt file (opened in main function),
def read_problem(f):
list_of_variables=[]
entry=0;
for line in f:
words = line.split()
#enters only if it's not a comment
if (words[0]!='#'):
if (words[0]=="VAR"):
x=variable;
elif (words[0]=="name"):
x.name=words[1]
list_of_variables.append(x)
for i in range(len(list_of_variables)):
print(list_of_variables[i].name)
return
My .txt file is:
VAR
name MaryCalls
VAR
name JohnCalls
VAR
name Burglary
VAR
name Earthquake
VAR
name Alarm
What I get in that print(and thus, the list) is:
Alarm
Alarm
Alarm
Alarm
Alarm
But I wanted to have:
MaryCalls
JohnCalls
Burglary
Earthquake
Alarm
What's wrong? Why are all the previous entries of the list changing?
The line x=variable makes the x refer to the class variable. You never create any instances of that class, instead you repeatedly modify a class-level variable name.
At the end of the program, when you print, variable.name of course has the last value assigned to it, in this case 'Alarm'.
You'll see this if you do print(list_of_variables):
[<class '__main__.variable'>, <class '__main__.variable'>,
etc.
Change x = variable to x = variable(), and you'll see (for example):
[<__main__.variable object at 0x6ffffee65d0>, <__main__.variable object at 0x6ffffee6610>
etc.
1) If you want to initialize the name in the constructor of variable, you have to indent the assignment further.
def __init__(self, name, name_alias, parents,values,table):
#name of the variable
self.name = ""
2) Your main problem is, that you want to create a new instance of variable with this line:
x=variable;
You have to write:
x = variable();
The primary problem is when the code detects a line starting with "VAR" it assigns the class variable to x, not an instance of the class. To do that you need to call the class which ends up invoking the class' __init__() method if it has one. Yours does, but it expects 5 arguments, none of which are known at that time of creation.
The easiest thing to do in such a case is to assign each argument a default value that means "doesn't have a value yet", and assign those to the instance being created (self).
Here's what I mean:
class Variable(object):
def __init__(self, name="", name_alias="", parents=None, values=None,
table=None):
self.name = name
self.name_alias = name_alias
self.parents = parents
self.values = values
self.table = table
def read_problem(f):
list_of_Variables=[]
for line in f:
words = line.split()
# if not a comment
if words[0] != '#':
if words[0] == "VAR":
x = Variable() # construct empty Variable
elif words[0] == "name":
x.name = words[1]
list_of_Variables.append(x)
for var in list_of_Variables:
print(var.name)
return
def main():
filename = 'variables.txt'
with open(filename) as f:
read_problem(f)
main()

How to change global variables in Python [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 3 years ago.
I am trying to change a variable further down the program. I have a global variable declared at the start of the program and I want to change the variable in different functions down the program. I can do this by declaring the variable inside the function again but I would like to know is there a better way of doing this. Some test code is below to explain what I mean.
ID = 'No'
project = ("Yep"+ID) # ID added with 'No' value which I later want to change
def pro():
ID = "YES"
print ID
def pro1(ID):
# I could declare project again to get this to work, but I would like to avoid this
print project # I want this to print with the new ID number.
if __name__ == '__main__':
pro()
pro1(ID)
Has anyone any ideas, thanks
I have tried the global variable but when I do this the project variable still prints out YepNo instead of YepYES. I want the new variable from the function proto change the variable in the project variable.
To update global variables you could use
global ID
ID="Yes"
before assigning variable to ID = "YES"
But changing ID will be no effect on project variable, project = ("Yep"+ID), because project is already a string
you need to make a function like
def getprojectname(ID):
return project+ID
The whole program may be like this
UPDATE:
... removed
Beware, you're doing it wrong multiple times.
Even though you could use the global statement to change a global (it is discouraged since it's better to use function parameters and return values), that would NOT change other already assigned values. E.g. even though you reassign ID, you would NOT reassign project. Also: your functions return nothing, there's no point in assigning a name to their return value, and it's a BAD habit using an all-uppercase name (ID) for a variable since it's a convention to use them for constants.
This should clarify you the way global works:
myid = ''
project = ("Yep"+myid) #ID added with no value which I later want to change
def mutate_id():
global myid
myid = "YES"
def mutate_project():
global project
project = ("YEP" + myid)
if __name__ == '__main__':
print "myid", myid
print "project ", project
print
mutate_id()
print "myid", myid
print "project ", project
print
mutate_project()
print "myid", myid
print "project ", project
print
But the best way is to do WITHOUT globals:
def get_new_id(old):
return "YES"
def get_new_project(old):
return ("YEP" + myid)
if __name__ == '__main__':
myid = ''
project = ("Yep"+myid)
print "myid", myid
print "project ", project
print
myid = get_new_id(myid)
print "myid", myid
print "project ", project
print
project = get_new_project(project)
print "myid", myid
print "project ", project
print
This will make all code interaction clear, and prevent issues related to global state change.
Use the global statement.
The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.
Example: http://www.rexx.com/~dkuhlman/python_101/python_101.html#SECTION004340000000000000000
P.S.
But don't use global too often, see http://www.youtube.com/watch?v=E_kZDvwofHY#t=10m45
In your code you have two problems. The first about changing ID variable, which could be solved by using global.
The second that your code calculate project string and after that project don't know about ID.
To avoid code duplication you can define function to calc project.
So we have:
ID = 'No'
def GetProject():
return "Yep"+ID
def pro():
global ID
ID = "YES"
print ID
print GetProject()
pro()
print GetProject()
You can mutate without reassigning:
variables = {}
def pro():
if variables['ID'] == '':
variables['ID'] = 'default'
Why not use a dictionary?
>>> attr = {'start':'XXX', 'myid':'No'}
>>>
>>> def update_and_show(D, value = None):
... if value: D['myid'] = value
... print D['start'] + ' ' + D['myid']
...
>>> update_and_show(attr)
XXX No
>>> update_and_show(attr, 'Yes')
XXX Yes
>>> update_and_show(attr, 'No')
XXX No
>>>

Categories