adamz88
adamz88

Reputation: 319

How to apply a function/method to multiple variables?

Often in my code I need to apply the same function or method to multiple variables. Traditionally, I do so as follows:

begin_range = row[0].replace(' ', '')
end_range = row[1].strip().replace(' ', '')

or

begin_range, end_range = row[0].replace(' ', ''), row[1].strip().replace(' ', '')

Is there a way to call the function/method for multiple variables in a way that the function is only written once?

Upvotes: 0

Views: 114

Answers (1)

AbdolHosein
AbdolHosein

Reputation: 558

It's possible in lots of ways!

The simplest one could be something like follows:

def usual_task(rows):
    return rows[0].replace(' ', ''), rows[1].strip().replace(' ', '')

and simply use it any place else as follows:

begin_range, end_range = usual_task(row)

If there is a pattern on remained data, you could apply much more complex function or other stuffs to keep you code simple and easy to read!

Upvotes: 1

Related Questions