0

I have a program like :

class ABC: q = {} def update: self.state = (xx,xx) global q if self.state in q: // do something 

I am getting the error :

"NameError: global name 'q' is not defined"

Im new to python and need some help.

1
  • Your code is not valid python, so it is difficult to gauge what your intent is here. Commented Sep 26, 2016 at 3:02

2 Answers 2

2

You can move q outside of the class:

q = {} class ABC: def update: self.state = (xx,xx) global q if self.state in q: # do something pass 

or you can reference q as a class variable:

class ABC: q = {} def update: self.state = (xx,xx) if self.state in ABC.q: # do something pass 
Sign up to request clarification or add additional context in comments.

1 Comment

ooh, Thanks Raymond, i think i get it. but why would i choose one over the other?
1

q isn't being declared as a global variable here - it's being declared as a class variable of class ABC.

If you want q to be global, you should define it before you start declaring the class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.