Using the Python decorator without the trouble of calling the parent class constructor, you can refer to the following code:
The code is as follows:
Class T1 (threading. Thread ):
Def _ init _ (self, a, B, c ):
Super (T1, self). _ init __()
Self. a =
Self. B = B
Self. c = c
Def run (self ):
Print self. a, self. B, self. c
The code defines a class inherited from threading. Thread.
Super (T1, self). _ init __()
Some people like to write it like this.
Threading. Thread. _ init _ (self)
Of course, the role is to call the constructor of the parent class.
After writing python code for so long, every time I write it, I feel like repeating the wheel. Just now I suddenly thought of the decorator. I tried to write an autoInitClass to help pythoner break away from the bitter sea, saving the trouble of manually calling the parent class constructor.
The code is as follows:
The code is as follows:
Def autoInitClass (OldClass ):
SuperClass = OldClass. mro () [1]
Class NewClass (OldClass ):
Def _ init _ (* args ):
Self = args [0]
SuperClass. _ init _ (self)
Apply (OldClass. _ init __, args)
Return NewClass
Use the autoInitClass modifier to construct a new class:
The code is as follows:
@ AutoInitClass
Class T2 (threading. Thread ):
Def _ init _ (self, a, B, c ):
# Super (T2, self). _ init __()
Self. a =
Self. B = B
Self. c = c
Def run (self ):
Print self. a, self. B, self. c
This article is from: itianda's blog. For more information, see source of the original article.