I get the following error:
Warning (from warnings module):
File "C:\Python34\projectEuler\projectEuler.py", line 316
global primeSet, primeList, primeCap, primeRan
SyntaxWarning: name 'primeRan' is used prior to global declaration
For the code:
primeSet = {2, 3}
primeList = [2, 3]
primeCap = 3
primeRan = False
def primeGen():
if primeRan:
primeList, primeCap = primeList, PrimeCap
global primeSet
else:
global primeSet, primeList, primeCap, primeRan
primeRan = True
for i in primeList:
yield i
while(True):
primeCap += 2
m = int(primeCap**.5)
yesPrime = True
for p in primeList:
if p > m: break
if primeCap%p == 0:
yesPrime = False
break
if yesPrime:
primeSet.add(primeCap)
primeList.append(primeCap)
yield primeCap
The variable is not written until it is assigned. And the code seems to work. Is the syntax message a false alarm, or should a global be declared before being read? (instead of only declaring before being written)
The code:
def primeGen():
global primeRan
if primeRan:
primeList, primeMax = primeList, PrimeCap
global primeSet
else:
global primeSet, primeList, primeCap
primeRan = True
Gets rid of the SyntaxWarning
. But it seems wrong to make the global deceleration for a value that is only being read and not written.
Should I ignore the syntax alarm?