2

For Python variable (e.g. List and integer) if we do not initialize it, are they always None? Is there any scenario Python will do initialization for us even if we do not initialize it explicitly?

For initialization, I mean,

Foo = []
Goo = 0

thanks in advance, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • In JavaScript an undefined variable returns `undefined` but in Python, it would raise a fatal error. – jkd Apr 15 '15 at 01:43

1 Answers1

5

A variable is defined when it is first assigned to a value. Typically, this follows the convention of variable = value. It doesn't become defined until this point, and is defined from this point on until the end of its scope.

If a variable hasn't been defined, attempting to read its data will raise a NameError. On the other hand, [], 0, and None are different types of data values that a defined variable can equal.

Specifically:

  • [] - An array with no elements
  • 0 - The int value equal to the number 0
  • None - A special data type which is meant to denote that a variable does not have a value. This is very different than a variable not being defined.

Python will not initialize a variable automatically -- how could it? Without knowing what kind of data or values will be operated on, Python can't handle an undefined variable. So it throws an exception, specifically a NameError.

  • 3
    Or an `UnboundLocalError` – Shashank Apr 15 '15 at 01:14
  • @dr_andonuts, what do you mean "variable not being defined"? An example is appreciated. Thanks. :) – Lin Ma Apr 15 '15 at 04:15
  • @LinMa When a variable is assigned a value, it becomes defined. Until that point, it is undefined. – The name's Bob. MS Bob. Apr 15 '15 at 05:14
  • @dr_andonuts, in what scenario Python will automatically assign/initialize None to a variable? No such scenario? Thanks. – Lin Ma Apr 15 '15 at 07:00
  • @LinMa Python will *never* automatically assign `None` to a variable. It will never automatically assign *any* value to a variable. – The name's Bob. MS Bob. Apr 15 '15 at 15:30
  • @dr_andonuts, thanks for all the comments, and how do we check if an variable is defined? I want to handle situation like if variable is assigned to None or not defined, I will trigger some corner case handling logics. Thanks. – Lin Ma Apr 15 '15 at 20:16
  • @LinMa if you want to check if a variable holds the value `None`, then you can use `if var is None`. You should never, *never*, *_never_* be in a situation where you are unsure if a variable has been defined or not. – The name's Bob. MS Bob. Apr 15 '15 at 21:23
  • @LinMa Also, if you found this answer/comments helpful, you should accept the answer so other people can benefit from it. – The name's Bob. MS Bob. Apr 15 '15 at 21:24
  • @dr_andonuts, for sure, the issue is someone calling my method (suppose I write an SDK), and they may pass a variable without defined (it is their side issue, and I want to be safe to check it is undefined). Is there a way to check if a variable is undefined? – Lin Ma Apr 16 '15 at 05:02
  • @dr_andonuts, for sure, if there is way to check undefined variable, I will make it as answered. :)) – Lin Ma Apr 16 '15 at 05:03
  • @LinMa A discussion of checking whether variables are defined is here: http://stackoverflow.com/questions/1592565/determine-if-variable-is-defined-in-python. Note however that this *should never be done*. You should always know at a point in your code if a variable is defined or not. – The name's Bob. MS Bob. Apr 16 '15 at 05:21
  • @ dr_andonuts, thanks, but how do I know if a variable is defined or not if it is a method input variable by others? – Lin Ma Apr 16 '15 at 08:25