简体   繁体   中英

How to pass a list of parameters to a function in Python

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])

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/

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....)

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM