Get stream variable arguments

I have a very simple question:

myThread = Thread(target=TestTarget, args=(1, Event(),))

      

Is it possible to get arguments with just a variable myThread

?

Thank!

+3


source to share


3 answers


Following the pilots answer - I am using this working solution:

from threading import Thread

class myThread(Thread):

    args = None

    def __init__(self, group=None, target=None, args=(), name=None, kwargs = None, daemon = None):
        self.args = args
        super(RaThread, self).__init__(group=group, target=target, args=args, name=name, kwargs=kwargs, daemon=daemon)

      



Thanks everyone for the help!

0


source


_Thread__args

and _Thread__kwargs

store the constructor arguments.

However, as you might guess from the underscores, they are not part of the public API. Indeed, "malformed, renamed attributes" are intended to prevent direct access.



Also, these attributes are specific to the CPython implementation. For example, Jython does not expose these attributes by these names (disclaimer: I haven't tested, but just looked at the source).

In your case, it would be better to store the arguments in some application-meaningful way in a Thread subclass and access them.

+3


source


You can simply use _Thread__arg

for an object Thread

to get the details of the arguments passed to that object Thread

.

import threading

def TestTarget(a, b):
    pass

myThread = threading.Thread(target=TestTarget, args=(1, 2,))

print myThread._Thread__arg

>>> (1, 2)

      

+1


source







All Articles