0

I warped a class in this way:

import Queue
import threading

class MyThread():
    q = Queue.Queue()
    content = []
    result = {}
    t_num = 0
    t_func = None
    def __init__ (self, t_num, content, t_func):
        for item in content:
            self.q.put(item)
        self.t_num = t_num
        self.t_func = t_func

    def start(self):
        for i in range(self.t_num):
            t = threading.Thread(target=self.worker)
            t.daemon = True
            t.start()
        self.q.join()
        return self.result

    def worker(self):
        while True:
            item = self.q.get()
            value = self.t_func(item)
            self.result[item] = value
            self.q.task_done()


x = [5, 6, 7, 8, 9]
def func(i):
    return i + 1

m = MyThread(4, x, func)
print m.start()

It works well. If I design the function func with 2 or more parameters, and pass these parameters in a list to the class, how can I call the func function in the function worker properly?

eg.

def __init__ (self, t_num, content, t_func, t_func_p):
            for item in content:
                self.q.put(item)
            self.t_num = t_num
            self.t_func = t_func
            self.t_func_p = t_func_p

def func(i, j, k):
m = MyThread(4, x, func, [j, k])
1
  • 1
    Can you give an example of the code you want to be able to write? Commented Jun 11, 2012 at 11:39

3 Answers 3

1

You need to use *args and **kwargs to pass any number of parameters to a function.

Here is more info: http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this might help:

def __init__(self, t_num, content, func, *params):
    func(*params) # params is a list here [param1, param2, param3....]


def func(param1, param2, param3):
# or 
def func(*params): # for arbitrary number of params

m = MyThread(4, x, func, param1, param2, param3....)

Comments

0

As a general rule, if you are going to be passing many parameters to a particular function, you may consider wrapping them into a simple object, the reasons are

  1. If you ever need to add/remove parameters, you just need to modify the object, and the function itself, the method signature (and all its references) will remain untouched
  2. When working with objects, you will always know what your function is receiving (this is specially useful if you are working on a team, where more people will use that function).
  3. Finally, because you control the creation of the object on its constructor, you can ensure that the values associated with the object are correct (for example, in the constructor you can make sure that you have no empty values, or that the types are correct).

If still you want to go with multiple parameters, check the *args and **kwargs, although I personally do not like that, as it may end up forcing people to read the function's source in order to use it.

Good luck :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.