[Python-ideas] Inline Functions - idea

Jan Kaliszewski zuo at chopin.edu.pl
Wed Feb 5 22:22:59 CET 2014


05.02.2014 20:48, Alex Rodrigues wrote:

> Let's say that I am a weather forecaster and I maintain a large
> library of different simulation techniques. All the techniques need 
> to
> get their initial data from some database. Let's take a look at some
> way of writing this code and their advantages/disadvantages:
>
> 	* Hard code the startup code into each simulation - This works, but
> it will lead to lots of code re-use and all the problems that come
> with that (i.e. if I later need to change how the startup values are
> downloaded, now I have to change every single function)
> 	* Use a closure - This isn't really an option, both because there
> are multiple functions and because closure cannot create variables in
> their containing scope
> 	* Return the values from a function - This will add lots of
> unnecessary typing at the top of each function to list out all of the
> variables and if I wish to add a new input to one simulation (maybe 
> UV
> just became important) I must now modify all of the functions to have
> that value passed in.
> 	* Classes - This is probably the best option currently available.
> Make a simulation class that has a initialize function and have each
> simulation inherit from it. This also has some drawbacks though, 
> since
> I must now instantiate a copy of the object to find the output of the
> simulation, which makes calling the function harder (especially on a
> one time basis).
> 	* Use an inline def - This is very modular and maintainable with an
> absolute minimum of boilerplate. The main downside being that it is
> not explicit what is available in the namespace once you initialize
> the simulation.
>
> Here's how it might look:
>
> inline def start_simulation():
>  date = datetime.utcnow()
>  date.replace(tzinfo=utc)
>  start_data = 
> urllib2.urlopen('http://www.source.com/seed_sims').read().split()
>  num_clouds = int(start_data[0])
>  temp = float(start_data[1])
>  grid = []
>  for a in range(int(start_data[2])):
>  grid.append([])
>  for b in range(int(start_data[3])):
>  grid[a].append(0)
>
> def sim_basic():
>  start_simulation()
>  return temp
>
> def sim_iterative():
>  start_simulation()
>  for row in grid:
>  for cell in row:
>  cell = temp+random.random()
>  for i in range(10):
>  for row in grid
>  for cell in row:
>  cell += random.random()-0.5
>  return average([cell for row in grid for cell in row])
>
> def sim_clouds():
>  start_simulation()
>  return temp - numclouds/10.

Please note, that it can already be implemented in the following way:

     class Simulation:
         def __init__(self):
             self.date = datetime.utcnow().replace(tzinfo=utc)
             self.start_data = urllib2.urlopen(
                 'http://www.source.com/seed_sims').read().split()
             self.num_clouds = int(self.start_data[0])
             self.temp = float(self.start_data[1])
             self.grid = []
             for a in range(int(self.start_data[2])):
                 self.grid.append([])
                 for b in range(int(self.start_data[3])):
                     self.grid[a].append(0)


     def sim_basic():
         sim = Simulation()
         return sim.temp

     def sim_iterative():
         sim = Simulation()
         for row in sim.grid:
             for cell in row:
                 cell = sim.temp + random.random()
         for i in range(10):
             for row in sim.grid:
                 for cell in row:
                     cell += random.random()-0.5
         return average(cell
                        for row in sim.grid
                            for cell in row)

     def sim_clouds():
         sim = Simulation()
         return sim.temp - sim.numclouds/10.

I believe it's better as here simulation state variables are explicitly
differentiated from actual local variables, so the code is much more
readable.

Cheers.
*j



More information about the Python-ideas mailing list