The idea behind this message is for the sake of readability. We expect to find all the attributes an instance may have by reading its __init__
method.
You may still want to split initialization into other methods though. In such case, you can simply assign attributes to None
(with a bit of documentation) in the __init__
then call the sub-initialization methods.
Just return a tuple from parse_arguments()
and unpack into attributes inside __init__
as needed.
Also, I would recommend that you use Exceptions in lieu of using exit(1)
. You get tracebacks, your code is reusable, etc.
class Wizard:
def __init__(self, argv):
self.name,self.magic_ability = self.parse_arguments(argv)
def parse_arguments(self, argv):
assert len(argv) == 2
return argv[0],argv[1]
Although the definition of instance variables outside init isn't recommended in general, there are rare cases in which it is natural. For example, when you have a parent class that defines several variables that its child classes won't use, and whose definition will make its child waste time or resources, or will be simply unaesthetic.
One possible solution to this is using an init-extention function, that each child class may override, and in this function use function setattr in order to define the class-unique instance variables. May be this is not too aesthetic as well, but it eliminates the here-discussed linting warning.
The best practice to solve this question is you need to build the parameter in Init part first, Then adjust it in the Def
class MainApplication(tk.Frame):
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.settingsFrame = None
self.create_widgets(master)
def create_widgets(self, master):
# frame Container
self.settingsFrame = tk.Frame(self.master, width=500, height=30, bg='white')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With