python - How to def a method with passing it a bool function? -


i have task should program in python. should have class named tasks. can

class tasks: 

code. class should have 4 method, 1 method needed pass logical function it, go on of elements in inf named file , count appropriate values in inf file, , should write out. question how can define function parameter should defined logical function?

here example: there file should call tasks class, example name "test". has following code:

def odd(x):    if x % 2 != 0 :         return true    else:        return false  task = tasks("in") //here have problem don't know how can open file tasks' constructor.  print task.count(odd) 

could please provide me solution this?

edit1: if inf file have following values:

30 25 30 21 19 

then task.count(odd) should print 3

there's nothing magical function's name, can pass function argument, invoke argument name in function.

for example, part of solution might include

class task:     def count(self, func):          file(self.inf, "r") src:              return [x x in src if func(x)]          return [] 

though function pass count in example have expect string ends in \n or \r\n depending on contents of inf file.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -