-2
def function()
    num = 1
    num += 1
    return num

Is num a bound variable or a free variable?

P.S. This code is written in python. There's no former code ahead of this.

  • 2
    Many argue that python does not have variables. You might consider the *object* instead. There are two objects in use here, 1 and 2. `num` is reference to the object, and is scoped within the function. – cdarke Dec 31 '14 at 09:44
  • You should surely read: [Python internals: Symbol tables, part 1](http://eli.thegreenplace.net/2010/09/18/python-internals-symbol-tables-part-1) – Ashwini Chaudhary Dec 31 '14 at 09:49
  • @cdarke One simply cannot sat Python doesn't have variables because it work differently than C when it works almost identical to how variables works in Lisp. Those objects are in fact what the variable `num` resolves to at different stages but not the variable itself. – Sylwester Dec 31 '14 at 09:53
  • @Sylwester: I said *Many argue* that python does not have variables, I don't argue that, but I can see their point. – cdarke Jan 02 '15 at 08:10

1 Answers1

1

From python doc : If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.

Here num is defined in function and as such is bound to it.

You can look to this other post from SO to have an example of free variable

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252