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
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
When the line
l = j + k
is exceuted, Python does roughly the following:
Load the objects currently bound to the names j and k. Here, this will be the integers 100 and 200.
Perform the addition. This will create a new int object with the value 300.
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.
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
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.
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:
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
j = 100
k = 200
l = j + k
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.