[Tutor] Beginner question (variables, namespaces...)
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sat Apr 8 02:02:54 CEST 2006
On Fri, 7 Apr 2006, Jesse wrote:
> Why is it that when one variable is assigned a value in terms of another
> variable, assigning a new value to the first doesn't change the value of
> the second?
Hi Jesse,
If you have a "variable" that depends on the values of other parameters,
that's just begging to be represented as a function. Let's look at some
of those computations:
> stock = float(raw_input("Enter stock (in terms of portions: "))
> container = float(raw_input("Enter portions per container: "))
> price_per_container = float(raw_input("Enter price per container: "))
> weekly_quota = float(raw_input("Enter quota (in terms of portions): "))
> extra = overstock(stock, weekly_quota)
> need = weekly_quota - extra
> buy_containers = roundup(need/container) # roundup rounds a non-integer to
> the next highest integer
> buy_portions = buy_containers * container
> leftover = buy_portions - need
> cost = price_per_container * buy_containers
Yeah, rather than code these as explicit variables, I'd strongly recommend
rewriting each of these as functions. Concretely:
####################################################
def extra(stock, weekly_quota):
return overstock(stock, weekly_quota)
def need(stock, weekly_quota):
return weekly_quota - extra(stock, weekly_quota)
def buy_containers(stock, weekly_quota, container):
return roundup(need(stock, weekly_quota) - extra(stock, weekly_quota))
...
####################################################
It's a little ugly, but this coding explicitely defines the dependencies
between the inputs into our system and the expected outputs. When we look
at buy_containers(), we can easily see that any change in the stock,
weekly_quote, or container parameters may have some profound affect on the
output to buy_containers().
That dependency between input and output is what we try to capture when we
write a function. So if you recode your assignment statements as function
definitions, you should be better able to handle changes to your input.
If we think of the names we use for these concepts, there's something
ironic that functions behave better on varying data than variables. Oh
well.
As an advanced aside: the "equations" that you're writing into Python,
unfortunately, aren't treated as real math equations, but as separate,
independent assignment statements. I think I know what you want, but
Python doesn't provide it out of the box. What I think you want is called
"constraint" programming. Such systems do exist. For example:
http://www.logilab.org/projects/constraint
I can't vouch for the maturity of logilab's "constraint" module; I haven't
played with it yet.
But if you're interested, you may want to look at the treatment of simple
constraint systems in the classic textbook "Structure and Interpretation
of Computer Programs":
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-22.html#%_sec_3.3.5
It should be very possible to adapt the material there into Python,
although it might take a fair bit of work if you're not familiar with
Scheme.
Good luck to you!
More information about the Tutor
mailing list