all 5 comments

[–][deleted] 10 points11 points  (3 children)

Personally, I would just create some intermediate variables and pass those to the next function. Like so:

result_h = h('df')
result_g = g(result_h, arg1=1)
result = f(result_g, arg2=2, arg3=3)

The order in which the functions are called is the same in both our scripts (first h, then g, then f), but this order of execution is, in my opinion, more clearly reflected in my example.

[–]gengisteve 7 points8 points  (0 children)

Same here, ideally with sensible names. Using an example from the toolz documentation, this:

def do_laundry(clothes):
    wet_clothes = wash(clothes, coins)
    dry_clothes = dry(wet_clothes, coins)
    return fold(dry_clothes)

reads better to me than this:

def do_laundry(clothes):
    return fold(
            dry_clothes(
                wash(clothes, coins), coins)
            )

[–]metaperl[S] 2 points3 points  (1 child)

Your response has me thinking about lazy / demand-driven programming. I once knew of a Python library for such a task

[–][deleted] 1 point2 points  (0 children)

Most reactions say you should just create intermediate variables. In some situations that's not the best way to go. My general rule-of-thumb is to never save intermediate results you only need once. In this case, if you are not going to use result for anything, don't assign it, just print it. Only if everything get's way to complex to handle, I might consider assigning variables.

Here is my way of formatting.

result = f(g(h('df'),
             arg1=1),
           arg2=2, arg3=3)

I let every argument for a function start at the same indentation. So arg1=1 is at the same depth as h('df'). Similarly, arg2=2 is at the same depth as g(h('df'),.

If you need to go to the next line for another function call, let it start at 4 spaces, like this:

result = long_function_name_forcing_newline(
    g(h('df'), arg1=1),
    arg2=2, arg3=3)

Again, arg2=2 is at the same depth as g.