【问题标题】:Passing variable length list as an argument to threaded function将可变长度列表作为参数传递给线程函数
【发布时间】:2017-11-11 08:05:31
【问题描述】:

我正在尝试将列表作为参数传递给线程函数。这个列表的长度可能会有所不同,但我不知道该怎么做。下面是我正在使用的代码。现在,我只想能够将列表放入线程函数中并在屏幕上打印出来。

#!/usr/bin/python
import threading
import Queue as queue


def generateRange(starting_chars=(*args)):
    print str(starting_chars)


for i in range(32,126):
    q = queue.Queue()
    theArgs = [i,32,32]
    threads = [ threading.Thread(target=generateRange,args=theArgs) ]
    # generateRange([i,32,32]) 
    for th in threads:
        th.daemon = True
        th.start()

我收到此错误:

    File "./test.py", line 6
    def generateRange(starting_chars=(*args)):
                                      ^
SyntaxError: invalid syntax

我做错了什么?

【问题讨论】:

    标签: python list parameter-passing python-multithreading


    【解决方案1】:

    你可以把语法改成这样,否则args不会被定义:

    def generateRange(*starting_chars):
        print str(starting_chars)
    

    【讨论】:

      【解决方案2】:

      我不知道您为什么特别希望将参数命名为“starting_chars”但是:

      def generateRange(*starting_chars):
          print str(starting_chars)
      

      或

      def generateRange(*args):
          print str(args)
      

      应该没问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        • 2011-09-30
        • 2021-11-03
        • 2013-10-29
        相关资源
        最近更新 更多