Jean Paul
Jean Paul

Reputation: 1588

False warning about name can be not defined in 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?

Upvotes: 4

Views: 6235

Answers (2)

zzcron toby
zzcron toby

Reputation: 31

You should ensure that TEST is initialized.

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

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81684

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

TEST = globals().get('TEST', 'test')
print(TEST)

Upvotes: 7

Related Questions