4

I want to pass an optional 'if' statement to a Python function to be executed. For example, the function might copy some files from one folder to another, but the function could take an optional condition.

So, for example, one call to the method could say "copy the files from source to dest if source.endswith(".exe")

The next call could be simply to copy the files from source to destination without condition.

The next call could be to copy files from source to destination if today is monday

How do you pass these conditionals to a function in Python?

Georgy
  • 12,464
  • 7
  • 65
  • 73
shoes_for_news
  • 43
  • 1
  • 1
  • 3

3 Answers3

14

Functions are objects. It's just a function that returns a boolean result.

def do_something( condition, argument ):
   if condition(argument):
       # whatever

def the_exe_rule( argument ):
    return argument.endswith('.exe')

do_something( the_exe_rule, some_file )

Lambda is another way to create such a function

do_something( lambda x: x.endswith('.exe'), some_file )
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • Another option is to use [`operator.methodcaller`](https://docs.python.org/3/library/operator.html#operator.methodcaller): `the_exe_rule = methodcaller('endswith', 'exe')`. You can add it in your answer if you want – Georgy Jul 09 '19 at 14:42
5

You could pass a lambda expression as optional parameter:

def copy(files, filter=lambda unused: True):
    for file in files:
        if filter(file):
            # copy

The default lambda always returns true, thus, if no condition is specified, all files are copied.

Jerub
  • 41,746
  • 15
  • 73
  • 90
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

Think you can use something like this:

def copy_func(files, destination, condition=None):
    for fileName in files:
        if condtition is None or condition(fileName):
            #do file copy

copy_func(filesToCopy, newDestionation) # without cond
copy_func(filesToCopy, newDestionation, lambda x: x.endswith('.exe')) # with exe cond
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
  • If you bind a `lambda` to a name, you should make it a function in the first place. – Björn Pollex Jul 12 '11 at 13:45
  • 1
    well, using `lambda` does make it a function. But it's correct to say you should just use `def` in this case. The first line is exactly equivalent to `def mondayCond(today): return isMonday(today)` – RoundTower Jul 12 '11 at 13:47
  • Your lambdas need to take the same arguments in order for copy_func to use them. Probably they should both take the filename (x), and isMonday would just ignore it and get today itself (not from the arg) – Lou Franco Jul 12 '11 at 13:49
  • Thank you all -i understand where i was wrong and modified my answer – Artsiom Rudzenka Jul 12 '11 at 13:58
  • @RoundTower but why i should use def if i simply want to use oneline function? – Artsiom Rudzenka Jul 12 '11 at 14:00
  • 1
    @Artsiom: [This excellent answer](http://stackoverflow.com/questions/1892324/why-program-functionally-in-python/1892614#1892614) from the awesome Alex Martelli explains it very well (among other things). – Björn Pollex Jul 12 '11 at 14:41
  • @Space_C0wb0y - thank you , will take a look and try to avoid creating such methods in a future – Artsiom Rudzenka Jul 12 '11 at 14:46