The following python code will set a secret key if the user does not provide one
Code with NameError:
import secrets
import string
import os
class Config:
secret = os.environ.get('SECRET')
if not secret:
alphabet = string.ascii_letters + string.digits + string.punctuation
secret = ''.join(secrets.choice(alphabet) for _ in range(256))
However, if secret is not set, this will result in a NameError: name 'alphabet' is not defined
. What confuses me most is that the following variations of Config
work properly:
Config without join (no error):
class Config:
secret = os.environ.get('SECRET')
if not secret:
alphabet = string.ascii_letters + string.digits + string.punctuation
secret = alphabet * 10 # just repeat the alphabet 10 times as the password
Config with alphabet
in main scope (no error)
alphabet = string.ascii_letters + string.digits + string.punctuation
class Config:
secret = os.environ.get('SECRET')
if not secret:
secret = ''.join(secrets.choice(alphabet) for _ in range(256))
Troubleshooting I've tried
- Verify all indentation is correct (each block uses 4 spaces)
- Test on python 3.7, 3.8, and 3.9 (all show identical results)
- Test code in Python REPL to check for errors (identical results again)
- Test simpler variations (remove
os
,string
, etc.)
Question
What is causing the NameError
? Am I misunderstanding how the class scope behaves or is this a bug in python?