Checking for an "undefined" variable - newbie question

D

Dan Rawson

How do I check if a variable has been defined??

The following don't appear to work:

if variable:

if variable is None:


I have only one (ugly) solution:

try:
variable
except NameError:
...

which works, but is a bit clumsy if I just want to know if the thing already exists.

If there's a more elegant way to check this, that would be great!

Thanks . . . .

Dan
 
D

Duncan Booth

I have only one (ugly) solution:

try:
variable
except NameError:
...

which works, but is a bit clumsy if I just want to know if the thing
already exists.

You could check whether the name of the variable is present in 'locals',
'globals', or 'vars' as appropriate:
0
 
A

Alex Martelli

Dan said:
How do I check if a variable has been defined??

The following don't appear to work:

if variable:

if variable is None:


I have only one (ugly) solution:

try:
variable
except NameError:
...

This is the canonical solution. Personally, I find it quite nice
that an ugly architecture (such as one based on whether a variable
is already defined/initialized rather than initializing it at the
start with a unique value and testing for that!) requires an ugly
way of expressing it. Unfortunately there are nicer ones such as
"if 'variable' not in locals() and 'variable' not in globals():" but
at least they're verbose and slow:).


If you can't rule out (e.g.) None as a valid value for your
variable, just make a unique placeholder value such as [be
sure to use a MUTABLE value, else uniqueness is not guaranteed]:

class _uninitialized: pass

initialize your variable at the start of your scope with

variable = _uninitialized

and test at any point within that scope whether the variable
is still uninitialized or has been assigned an actual value by

if variable is _uninitialized:

It seems to me that this is far preferable to any approach
based on _not_ binding the name 'variable' at all and later
needing to test whether the name is bound or not.


Alex
 
D

Dan Rawson

Duncan said:
You could check whether the name of the variable is present in 'locals',
'globals', or 'vars' as appropriate:



0
Ahhhh . . . . thanks! 'locals' and 'globals' I think I understand, but what's the scope for 'vars' ??

In the specific case in question, these are global variables, so it's relatively easy . . . .

TIA . . .

Dan
 
D

Damien Wyart

* Alex Martelli said:
If you can't rule out (e.g.) None as a valid value for your variable,
just make a unique placeholder value such as [be sure to use a MUTABLE
value, else uniqueness is not guaranteed]:

Could you expand on this "uniqueness" point ? I don't see very well what
you mean.


Thanks in advance,
 
M

Michael Hudson

Damien Wyart said:
* Alex Martelli said:
If you can't rule out (e.g.) None as a valid value for your variable,
just make a unique placeholder value such as [be sure to use a MUTABLE
value, else uniqueness is not guaranteed]:

Could you expand on this "uniqueness" point ? I don't see very well what
you mean.

I think Alex is talking about things like this:
False

Two references to equal immutable objects might as well reference the
same object, and sometimes this happens (e.g. there is only *one* int
object for each integer in some range -- I think it's -10..100 in
today's Python) and sometimes it doesn't.

TBH, I'm not sure Alex is doing the OP a favour in bringing this
aspect of the implementation to his attention...

Cheers,
mwh
 
S

Steven Taschuk

Quoth Dan Rawson:
How do I check if a variable has been defined?? [...]
try:
variable
except NameError:
...

This is the answer to your question.
If there's a more elegant way to check this, that would be great!

Why do you want to check it in the first place?

In particular, why not initialize your variable to some
out-of-band value (such as None) and test that? E.g.,

myvar = None
# ... code which might set myvar to something else ...
if myvar is None:
# ...
 
M

Michele Simionato

Dan Rawson said:
How do I check if a variable has been defined??

The following don't appear to work:

if variable:

if variable is None:


I have only one (ugly) solution:

try:
variable
except NameError:
...

which works, but is a bit clumsy if I just want to know if the thing already exists.

If there's a more elegant way to check this, that would be great!

Thanks . . . .

Dan

IMHO, the try .. except trick is the easiest, efficient and Pythonic
solution. I would recommend it. If you don't like it, you may try

if variable in dict

where dict can be globals(), locals() or vars().

But still I would prefer the explicit try ... except.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top