Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:将函数传递到*args_Python_Python 3.x_Python 3.4 - Fatal编程技术网

Python 3:将函数传递到*args

Python 3:将函数传递到*args,python,python-3.x,python-3.4,Python,Python 3.x,Python 3.4,我正在使用\u thread模块(因为它更适合我的程序),我想将一个函数传递给我正在传递给线程的函数。但是,只能将元组传递到\u线程中。启动\u new\u thread()函数,但是,该函数不适用 @威廉·范昂森-这是完整的代码。使用Pygame class PGMaster: """ Properties: mainloopOn - exits program if False Functions: PGMaster.mainlooprunner(

我正在使用
\u thread
模块(因为它更适合我的程序),我想将一个函数传递给我正在传递给线程的函数。但是,只能将元组传递到
\u线程中。启动\u new\u thread()
函数,但是,该函数不适用


@威廉·范昂森-这是完整的代码。使用Pygame

class PGMaster:
    """
    Properties:
    mainloopOn - exits program if False

    Functions:
    PGMaster.mainlooprunner()
        Repeatedly runs Main function (must be overrided).
    PgMaster.mainloop(subthread=False)
        Wrapper for mainlooprunner. Subthread - will it run under MainThread? No if True.
    """

    def winmode(self, dimensions, flags=0, caption='', depth=None):
        """
        Flags are:
        pygame.FULLSCREEN    create a fullscreen display
        pygame.DOUBLEBUF     recommended for HWSURFACE or OPENGL
        pygame.HWSURFACE     hardware accelerated, only in FULLSCREEN
        pygame.OPENGL        create an OpenGL-renderable display
        pygame.RESIZABLE     display window should be sizeable
        pygame.NOFRAME       display window will have no border or controls
        """
        if depth != None:
            screenx = self.screen = pygame.display.set_mode(dimensions, flags, depth)
        elif depth == None:
            screenx = self.screen = pygame.display.set_mode(dimensions, flags)
        pygame.display.set_caption(caption)
        return screenx

    def __init__(self, dimensions, flags=0, caption='', depth=None):
        self.winmode(dimensions, flags, caption, depth)
        self.mainloopOn = True


    def mainlooprunner(self, function):
        func = function
        while self.mainloopOn:
            func()

    def mainlooprunnerx(self, *function):
        func = function[0]
        while self.mainloopOn:
            func()

    def mainloop(self, func, subthread=False):
        if subthread:
            functuple = tuple(func)
            _thread.start_new_thread(self.mainlooprunnerx, functuple)
        elif not subthread:
            self.mainlooprunner(func)
    . . .

if __name__ == '__main__':
    root = PGMaster([1600, 900], pygame.DOUBLEBUF | pygame.RESIZABLE)
    # vvv Real codes vvv
    print(pygame.FULLSCREEN)
    print(pygame.DOUBLEBUF)
    print(pygame.HWSURFACE)
    print(pygame.OPENGL)
    print(pygame.RESIZABLE)
    print(pygame.NOFRAME)
    # ^^^ Real codes ^^^

    def myloop():
        print('Hello World!')
        return
    root.mainloop(myloop, True)
输出:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\PyGameUI\__init__.py", line 127, in <module>
    root.mainloop(myloop, True)
  File "C:\Users\User\Desktop\PyGameUI\__init__.py", line 100, in mainloop
    functuple = tuple(func)
TypeError: 'function' object is not iterable
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Desktop\PyGameUI\\uuuu init\uuuuu.py”,第127行,在
mainloop(myloop,True)
文件“C:\Users\User\Desktop\PyGameUI\\uuuu init\uuuuu.py”,第100行,在mainloop中
functuple=元组(func)
TypeError:“函数”对象不可编辑

我知道了<代码>\u线程实际上是坏的。我已经重写了我的代码-现在我正在使用
线程



我使用了
tuple(func)
而不是
tuple((func,)

我不太清楚您希望实现什么,等等。您能给出一个输入示例和错误消息/预期输出吗?不要使用以下划线开头的任何内容。这是python将某些东西标记为“受保护”的方式,开发人员不能保证它的行为不会改变。@gonczor不赞成使用它,但我仍然使用它,因为我不想定义嵌套类-它会降低代码的可读性。从文档中可以看到:“由于线程被弃用,线程模块在_thread中被重命名了”。您需要的是
functuple=(func,)
,即一个1项元组,因此您更应该避免使用它!