Divino
Divino

Reputation: 47

Dynamic nature of python

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

Upvotes: 2

Views: 545

Answers (5)

nathan lachenmyer
nathan lachenmyer

Reputation: 5550

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

Upvotes: 1

Matt Alcock
Matt Alcock

Reputation: 12891

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.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613352

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.

Upvotes: 3

Marcelo Cantos
Marcelo Cantos

Reputation: 185980

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

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 602295

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.

Upvotes: 6

Related Questions