3


I am trying to port to python3 this answer. But I am not sure how to set theThread.__init__ function in python3.

The deceleration of a python2 Thread.__init__ in a thread class is:

Thread.__init__(self, group=None, target=None, name=None, args=(), kwargs=None, verbose=None)

But python3 its:

Thread.__init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)

Note: The ending in python3 is: args=(), kwargs=None, *, daemon=None) Part of the problem is I don't understand how to treat the asterisk * in the function deceleration. Explaining what it is for would also be helpful.

Just using the python2 raises: TypeError: __init__() takes from 1 to 6 positional arguments but 7 were given

How should I write the python3 Thread.__init__?

Update, the python2 call in the example is:

Thread.__init__(self, group, target, name, args, kwargs, Verbose)

How so my logic would say this would work in python3, but it doesn't, the answer should include how to call this, or instructions how to build it, including kwargs:

Thread.__init__(self, group, target, name, args, kwargs, daemon)
Community
  • 1
  • 1
GuySoft
  • 1,723
  • 22
  • 30

1 Answers1

1

If you need to overload it, just be careful how you construct your super call (or any call up to Thread.__init__) to be explicit about the arguments after the bare * (i.e provide them in key form):

For example, if you have a Foo class with a signature of:

class Foo:
    def __init__(self, b=30, *, c=40):
        print(b, c)

you'd need to provide a c=<value> argument whenever you invoke it. So, for example, if you overload it in a FooChild class by:

class FooChild(Foo):
    def __init__(self, a, myval_1, my_val2):
        print(a)
        super().__init__(b, c=my_val2)

you'll need to provide the name c when calling BaseClass.__init__.

If you have *args and **kwargs you'll again just need to explicitly provide a value for c when invoking the child class __init__, i.e:

class FooBase(Foo):
    def __init__(self, a, *args, **kwargs):
        print(a)
        super().__init__(*args, **kwargs)

Can be invoked by FooBase(1, 2, c=30) but not like FooBase(1, 2, d=30) or FooBase(1, 2, 30). You need to pass c to __init__, it requires it.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Can't I do something involving kargs? The example I provided uses it like this: `` Thread.__init__(self, group, target, name, args, kwargs, Verbose)``, where kwargs is used when calling ``Thread.__init__`` – GuySoft Oct 31 '16 at 11:27
  • Of course you can @GuySoft, you just always need to provide `c` to `Thread.__init__`; in what way you do that is irrelevant, you just need to pass it somehow. – Dimitris Fasarakis Hilliard Oct 31 '16 at 11:41
  • 1
    Ok got it all working! And I also posted the [full code answer on the thread](http://stackoverflow.com/a/40344234/311268) – GuySoft Oct 31 '16 at 14:29
  • 1
    That's great @GuySoft, upvoted. Porting popular, and somewhat complex, answers is something that more people should consider doing. – Dimitris Fasarakis Hilliard Oct 31 '16 at 15:34