4

What's the uninitialized value in Python, so I can compare if something is initialized, like:

val

if val == undefined ?

EDIT: added a pseudo keyword.

EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 1
    Can you tell us why you want to do that? There's probably a more "pythonic" way to accomplish what you are trying to do. – Dana Mar 19 '09 at 22:00
  • 1
    Since variables are just tags attached to objects, the whole question is very non-pythonic. The idea of "uninitialized" can't exist -- this question is quite strange. – S.Lott Mar 20 '09 at 00:48

9 Answers9

11

Will throw a NameError exception:

>>> val
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'val' is not defined

You can either catch that or use 'val' in dir(), i.e.:

try:
    val
except NameError:
    print("val not set")

or

if 'val' in dir():
    print('val set')
else:
    print('val not set')
phihag
  • 278,196
  • 72
  • 453
  • 469
  • There could be UnboundLocalError in local scope. – jfs Mar 19 '09 at 23:00
  • @J.F. Sebastian Huh? UnboundLocalError is a subclass of NameError, isn't it? – phihag Mar 19 '09 at 23:31
  • @phihag: Yes, it is. I've thought It is worth pointing out the difference between "UnboundLocalError: local variable 'val' referenced before assignment" and "NameError: global name 'val' is not defined". – jfs Mar 19 '09 at 23:51
  • You will get a ULE if val is assigned to some time later within the same function. – Ignacio Vazquez-Abrams Mar 20 '09 at 00:52
  • wrong answer. var = None is what he wants. See his duplicate question – hasen Mar 20 '09 at 04:17
  • @hasen j var = None might be what the orginal author wanted, but he or she is not the only one interested in this – phihag Mar 20 '09 at 09:55
5

In python, variables either refer to an object, or they don't exist. If they don't exist, you will get a NameError. Of course, one of the objects they might refer to is None.

try:
   val
except NameError:
   print "val is not set"


if val is None:
   print "val is None"
Joe Koberg
  • 25,416
  • 6
  • 48
  • 54
5

A name does not exist unless a value is assigned to it. There is None, which generally represents no usable value, but it is a value in its own right.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

This question leads on to some fun diversions concerning the nature of python objects and it's garbage collector:

It's probably helpful to understand that all variables in python are really pointers, that is they are names in a namespace (implemented as a hash-table) whch point to an address in memory where the object actually resides.

Asking for the value of an uninitialized variable is the same as asking for the value of the thing a pointer points to when the pointer has not yet been created yet... it's obviously nonsense which is why the most sensible thing Python can do is throw a meaningful NameError.

Another oddity of the python language is that it's possible that an object exists long before you execute an assignment statement. Consider:

a = 1

Did you magically create an int(1) object here? Nope - it already existed. Since int(1) is an immutable singleton there are already a few hundred pointers to it:

>>> sys.getrefcount(a)
592
>>>

Fun, eh?

EDIT: commment by JFS (posted here to show the code)

>>> a = 1 + 1
>>> sys.getrefcount(a) # integers less than 256 (or so) are cached
145
>>> b = 1000 + 1000
>>> sys.getrefcount(b) 
2
>>> sys.getrefcount(2000)
3
>>> sys.getrefcount(1000+1000)
2
jfs
  • 399,953
  • 195
  • 994
  • 1,670
Salim Fadhley
  • 22,020
  • 23
  • 75
  • 102
1

In Python, for a variable to exist, something must have been assigned to it. You can think of your variable name as a dictionary key that must have some value associated with it (even if that value is None).

David Locke
  • 17,926
  • 9
  • 33
  • 53
1

Q: How do I discover if a variable is defined at a point in my code?

A: Read up in the source file until you see a line where that variable is defined.

Jerub
  • 41,746
  • 15
  • 73
  • 90
1

Usually a value of None is used to mark something as "declared but not yet initialized; I would consider an uninitialized variable a defekt in the code

Ber
  • 40,356
  • 16
  • 72
  • 88
0
try:
    print val
except NameError:
    print "val wasn't set."
bryan
  • 2,223
  • 1
  • 22
  • 19
0

To add to phihag's answer: you can use dir() to get a list of all of the variables in the current scope, so if you want to test if var is in the current scope without using exceptions, you can do:

if 'var' in dir():
    # var is in scope
Community
  • 1
  • 1
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589