Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

False warning about name can be not defined in PyCharm

Tags:

python

pycharm

I wrote a python code to give a value to a variable if it is not defined, but nevertheless PyCharm warns me that the variable can be not defined:

if 'TEST' not in globals():
    TEST = "test"

print(TEST)

Name 'TEST' can be not defined

Is there an other way to define undefined variables such that PyCharm understand it?

like image 904
Jean Paul Avatar asked Nov 05 '25 04:11

Jean Paul


1 Answers

You can use a nonconditional initializer, e.g. with get, to get rid of the warning:

TEST = globals().get('TEST', 'test')
print(TEST)
like image 58
DeepSpace Avatar answered Nov 08 '25 00:11

DeepSpace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!