2

I know that I can create and start a thread in Python using the following syntax:

import threading

tracker_thread = threading.Thread(target=func, args=(x,y,z))
tracker_thread.daemon = True
tracker_thread.start()

What I need is to use a class constructor as target. I know that a solution for this is the following:

def wrap_function(x,y,z):
    MyClas(x,y,z)

tracker_thread = threading.Thread(target=wrap_function, args=(x,y,z))

But I am trying to avoid the use of a similar "wrap function", by using only the constructor in the initialization of Thread.

How can I do this?

1
  • 2
    threading.Thread(target=MyClass, args=(x, y, z)) didn't work? Commented May 23, 2015 at 20:00

1 Answer 1

1

Solved. The simplest solution is always the best:

tracker_thread = threading.Thread(target=MyClass, args=(x,y,z))

Thanks to Sir_FZ for the comment.

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

2 Comments

is it args=[x, y, z] or args=(x, y, z)?
Yeah -- Python "no special syntax for instantiating objects" wins again! The only time function/class interchange ever got me is in writing a decorator as a class, and use it to decorate a method in Python 3. (hint: you decorator will need a __get__ method)

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.