2

I am a newbie to programmimg and python. When I do:

 j = 100
 k = 200
 l = j + k
 l
 300

Now, if I change the value of J, I expect the output of 'l' to be different. Why is the output same and how can I make it change.

 j = 250
 l
 300
3
  • 1
    " I expect the output of 'l' to be different". Really? Why? Please explain what you expected and why. Commented Feb 22, 2012 at 23:11
  • 4
    Python is not a spreadsheet :-) Commented Feb 22, 2012 at 23:21
  • However, if you do want this behavior, you can take a look at the sympy (external) module for symbolic math. Commented Feb 23, 2012 at 3:30

5 Answers 5

6

When the line

l = j + k

is exceuted, Python does roughly the following:

  1. Load the objects currently bound to the names j and k. Here, this will be the integers 100 and 200.

  2. Perform the addition. This will create a new int object with the value 300.

  3. Bind the name l to the resulting object.

As you can see, l is just bound to an integer that doesn't "know" any more where it came from.

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

Comments

3

That's not what "dynamic" means. Once a value is assigned to a variable, the variable retains that value until it is assigned a different value.

Dynamic languages are those whose variables can take on any type at runtime, and usually can even hold multiple values of different types over their lifespan:

a = 1
print a
a = "hello"
print a

That said, you can achieve something similar to what you expected:

>>> j = 100
>>> k = 200
>>> l = lambda: j + k
>>> l()
300
>>> j = 250
>>> l()
450

Comments

3

In your code, l = j + k simply evaluates j + k, which results in an integer value of 300. Then the name l is bound to that int object of value 300.

The addition is evaluated once and once only. If you want a new addition to be performed then you will have to explicitly invoke it one way or another.

Comments

1

Structure and Intepretation of Programming (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.2) has a really good section on this.

When you use expressions like j = 100 or k = 200, you are basically giving a new 'name' to the value. This is an abstraction that you're making so that finding this number is easier later on. When you assign a value to a variable, the interpreter:

  1. Figures out (recursively) what is on the right side of the expression
  2. Resolves that to a value (or string, or int, etc.)
  3. Gives that value the 'name' that you are assigning it.

So in the case of l = j + k, the interpreter computes the value of j + k, then gives that value the name 'l'. l has no sense of where it came from, it's just pointing at whatever that value we just computed was.

What you're looking for is lambda. Lambda creates a procedure that is carried out every time you call it.

So you can create a procedure (function):

l = lambda j,k: j + k

where i and j are the arguments of the procedure. Every time you call the procedure, it looks at the inputs you've given it (this particular case has i and j as inputs) and then follows through with the procedure. Another potential candidate is:

l = lambda: j + k

this procedure takes no inputs, but every time it is called it looks up whatever value the variables j and k have been assigned, and then computes them.

The concept of variables vs. procedures vs. functions can be very confusing to the beginner programmer; this is a great question and I hope this answer was helpful :)

-Nathan Lachenmyer

Comments

0
j = 100
k = 200
l = j + k
  • j is a pointer to the memory holding 100
  • k is a pointer to the memory holding 200
  • l is a pointer to the memory holding 300

l is not a pointer to the unexcuted function of j + k as i think you were expecting.

changing j is not going to change the fact that l is pointing to 300.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.