Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop inside or outside a function?

What is considered to be a better programming practice when dealing with more object at time (but with the option to process just one object)?

A: LOOP INSIDE FUNCTION

Function can be called with one or more objects and it is iterating inside function:

    class Object:
        def __init__(self, a, b):
            self.var_a = a
            self.var_b = b

        var_a = ""
        var_b = ""

    def func(obj_list):
        if type(obj_list) != list:
            obj_list = [obj_list]

        for obj in obj_list:
            # do whatever with an object
            print(obj.var_a, obj.var_b)

    obj_list = [Object("a1", "a2"), Object("b1", "b2")]
    obj_alone = Object("c1", "c2")

    func(obj_list)
    func(obj_alone)

B: LOOP OUTSIDE FUNCTION

Function is dealing with one object only and when it is dealing with more objects in must be called multiple times.

    class Object:
        def __init__(self, a, b):
            self.var_a = a
            self.var_b = b

        var_a = ""
        var_b = ""

    def func(obj):
        # do whatever with an object
        print(obj.var_a, obj.var_b)

    obj_list = [Object("a1", "a2"), Object("b1", "b2")]
    obj_alone = Object("c1", "c2")

    for obj in obj_list:
        func(obj)
    func(obj_alone)

I personally like the first one (A) more, because for me it makes cleaner code when calling the function, but maybe it's not the right approach. Is there some method generally better than the other? And if not, what are the cons and pros of each method?

like image 562
atomic44 Avatar asked Jun 10 '15 14:06

atomic44


People also ask

Can you put a loop inside a function?

When we log a function call expression the output is the return value of the function. We logged the return value of a self-invoking (it called itself) anonymous function expression. This proves that we can run a function inside a loop.

Can we use for loop inside function in Python?

For loops can iterate over any iterable object (example: List, Set, Dictionary, Tuple or String). Now with the help of the above example, let's dive deep and see what happens internally here. Make the list (iterable) an iterable object with help of the iter() function.

Can you put a for loop inside a list?

For example, you can loop through a list to check if the elements meet certain conditions. You can also have a for loop inside another for loop. In this example, for every element in the first list, we loop through all elements in the second list.

Are for loops inclusive?

For loops are always inclusive in Python: they always run over all elements of the iterator (other than exceptions such as break , etc.).


2 Answers

A function should have a defined input and output and follow the single responsibility principle. You need to be able to clearly define your function in terms of "I put foo in, I get bar back". The more qualifiers you need to make in this statement to properly describe your function probably means your function is doing too much. "I put foo in and get bar back, unless I put baz in then I also get bar back, unless I put a foo-baz in then it'll error".

In this particular case, you can pass an object or a list of objects. Try to generalise that to a value or a list of values. What if you want to pass a list as a value? Now your function behaviour is ambiguous. You want the single list object to be your value, but the function treats it as multiple arguments instead.

Therefore, it's trivial to adapt a function which takes one argument to work on multiple values in practice. There's no reason to complicate the function's design by making it adaptable to multiple arguments. Write the function as simple and clearly as possible, and if you need it to work through a list of things then you can loop it through that list of things outside the function.

This might become clearer if you try to give an actual useful name to your function which describes what it does. Do you need to use plural or singular terms? foo_the_bar(bar) does something else than foo_the_bars(bars).

like image 63
deceze Avatar answered Oct 10 '22 12:10

deceze


Move loops outside functions (when possible)

Generally speaking, keep loops that do nothing but iterate over the parameter outside of functions. This gives the caller maximum control and assumes the least about how the client will use the function.

The rule of thumb is to use the most minimal parameter complexity that the function needs do its job.

For example, let's say you have a function that processes one item. You've anticipated that a client might conceivably want to process multiple items, so you changed the parameter to an iterable, baked a loop into the function, and are now returning a list. Why not? It could save the client from writing an ugly loop in the caller, you figure, and the basic functionality is still available -- and then some!

But this turns out to be a serious constraint. Now the caller needs to pack (and possibly unpack, if the function returns a list of results in addition to a list of arguments) that single item into a list just to use the function. This is confusing and potentially expensive on heap memory:

>>> def square(it): return [x ** 2 for x in it]
...
>>> square(range(6)) # you're thinking ...
[0, 1, 4, 9, 16, 25]
>>> result, = square([3]) # ... but the client just wants to square 1 number
>>> result
9

Here's a much better design for this particular function, intuitive and flexible:

>>> def square(x): return x ** 2
...
>>> square(3)
9
>>> [square(x) for x in range(6)]
[0, 1, 4, 9, 16, 25]
>>> list(map(square, range(6)))
[0, 1, 4, 9, 16, 25]
>>> (square(x) for x in range(6))
<generator object <genexpr> at 0x00000166D122CBA0>
>>> all(square(x) % 2 for x in range(6))
False

This brings me to a second problem with the functions in your code: they have a side-effect, print. I realize these functions are just for demonstration, but designing functions like this makes the example somewhat contrived. Functions typically return values rather than simply produce side-effects, and the parameters and return values are often related, as in the above example -- changing the parameter type bound us to a different return type.

When does it make sense to use an iterable argument? A good example is sort -- the smallest unit of operation for a sorting function is an iterable, so the problem of packing and unpacking in the square example above is a non-issue.

Following this logic a step further, would it make sense for a sort function to accept a list (or variable arguments) of lists? No -- if the caller wants to sort multiple lists, they should loop over them explicitly and call sort on each one, as in the second square example.

Consider variable arguments

A nice feature that bridges the gap between iterables and single arguments is support for variable arguments, which many languages offer. This sometimes gives you the best of both worlds, and some functions go so far as to accept either args or an iterable:

>>> max([1, 3, 2])
3
>>> max(1, 3, 2)
3

One reason max is nice as a variable argument function is that it's a reduction function, so you'll always get a single value as output. If it were a mapping or filtering function, the output is always a list (or generator) so the input should be as well.

To take another example, a sort routine wouldn't make much sense with varargs because it's a classically in-place algorithm that works on lists, so you'd need to unpack the list into the arguments with the * operator pretty much every time you invoke the function -- not cool.

There's no real need for a call like sort(1, 3, 4, 2) as there is with max, where the parameters are just as likely to be loose variables as they are a packed iterable. Varargs are usually used when you have a small number of arguments, or the thing you're unpacking is a small pair or tuple-type element, as often the case with zip.


There's definitely a "feel" to when to offer parameters as varargs, an iterable, or a single value (i.e. let the caller handle looping), but as long as you follow the rule of avoiding iterables unless they're essential to the function, it's hard to go wrong.

As a final tip, try to write your functions with similar contracts to the library functions in your language or the tools you use frequently. These are pretty much always designed well; mimic good design.

like image 21
ggorlen Avatar answered Oct 10 '22 13:10

ggorlen