Reputation: 411
i saw this phrase in python 2.6 man:
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
.
.
.
If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()
) before doing anything else to the thread.
i just wanna know why?
Upvotes: 0
Views: 450
Reputation: 882421
If you want to know "exactly what, in detail, does the superclass's __init__
do for me", you can always study the sources and see, e.g., ...:
self.__target = target
self.__name = str(name or _newname())
self.__args = args
self.__kwargs = kwargs
self.__daemonic = self._set_daemon()
self.__ident = None
self.__started = Event()
self.__stopped = False
self.__block = Condition(Lock())
self.__initialized = True
the exact set of super-private variables being initialized may of course vary from one revision of python to the next, but obviously there are always going to be some such variables and/or other initialization stuff performed in the base class's __init__
. You only need to know all details for your own instruction (you should "respect" abstraction layers you choose to program on, but, if you're a wise and experienced developer, the better you understand what those abstraction layers are wisely hiding, the more you'll respect them;-).
In general, the rule should always be for each __init__
to invoke the superclass's, except only very specific cases (such as "transparent mixin classes" -- and if you don't know what they are, you most likely don't need to know). The general rule will always be "that superclass initialization may or may not be important in this specific release, but it will never hurt and will often be necessary and/or helpful"!-)
Upvotes: 2
Reputation:
Because Thread.__init__
sets some internal variables which will be undefined if you omit the call. In general, it is a good practice to invoke super constructor in all cases unless there is a strong reason to not do it.
Upvotes: 3