-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.

4
  • 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. Commented Dec 31, 2014 at 9:44
  • You should surely read: Python internals: Symbol tables, part 1 Commented Dec 31, 2014 at 9: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. Commented Dec 31, 2014 at 9:53
  • @Sylwester: I said Many argue that python does not have variables, I don't argue that, but I can see their point. Commented Jan 2, 2015 at 8:10

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.