Passing data between objects and calling all objects of a class in turn
python at mrabarnett.plus.com
Wed Aug 25 05:18:52 CEST 2010
On 25/08/2010 03:49, ghoetker wrote:
> I'm a fairly new Python coder, learning along with my son (actually,
> hopefully a bit ahead of him...). We're stuck on something.
> As part of solving a backwards induction problem (purely as a learning
> experience, we are geeks), we are going to create N objects, each of
> the class "interview". We will first create the N_th interview, which
> will calculate, based on data we pass to it, its expected payoff
> value. We will then create interview N-1, which needs to know the
> expected payoff of interview N in order to determine a decision and
> then calculate its own expected payoff. We then create interview N-2,
> which needs to know the expected payoff of interview N-1, etc., until
> we reach interview 1.
> 1. How can we best (that is, most Pythonically) let interview N-1
> know the expected value of interview N and so on. One way would be to
> define a variable "payoff_of_continuing" which gets set to the
> expected value of interview N and is passed to interview N-1 when is
> created. Interview N-1 then resets "payoff_of_continuing", which is
> passed to interview N-2 when it is created, etc. Is there a more
> Pythonic approach?
I hope you don't mean that "payoff_of_continuing" is a global variable
which is set by the instance when it's created. The Pythonic way would
be to create the instance and then ask it what it's payoff is so that
you then know what to pass in when you create the next instance.
> 2. When we want to output the results of the analysis, how do we most
> Pythonically get each interview object in turn to report its identity,
> the result of its decision and its expected payoff (e.g., For
> interview 3, stop if you interview a good candidate. This interview's
> expected value is 2.5). I know how to do the reporting part
> (print....), it's the "getting each interview object in turn to..."
> part that I'm unsure about. We could have each interview report as it
> is created, but I'm wondering if the splitting it into "Create all the
> objects" and "Report the results from all the objects" is sensible.
> For one thing, it would allow compiling the results into a table, etc.
Put the objects into a list when you create them and then iterate through
them, asking each to print its report.
> I really appreciate any input. I'm a competent procedural programmer
> (albeit not primarily in Python), but a dabbler in object oriented
> programming (we've already done this problem procedurally, but want to
> try it again as OOP). Thank you!!
More information about the Python-list