Pete
Pete

Reputation: 1303

Why does PyLint warn about no __init__?

We have numerous python classes that do not seem to need __init__, initialising them empty is either perfectly acceptable or even preferable. PyLint seems to think this is a bad thing. Am I missing some insight into why having no __init__ is a Bad Smell? Or should I just suppress those warnings and get over it?

Upvotes: 22

Views: 10259

Answers (2)

Andrew Clark
Andrew Clark

Reputation: 208495

What are you using these classes for?

If they are just a grouping of functions that do not need to maintain any state, there is no need for an __init__() but it would make more sense to just move all of those functions into their own module.

If they do maintain a state (they have instance variables) then you should probably have an __init__() so that those variables can be initialized. Even if you never provide values for them when the class is created, it is generally a good idea to have them defined so that your method calls are not referencing instance variables that may or may not exist.

That being said, if you don't need an __init__(), feel free to ignore that warning.

edit: Based on your comment, it seems like you are fine with the AttributeError you will get on referencing variables before initialization. That is a perfectly fine way to program your classes so in that case ignoring the warning from PyLint is reasonable.

Upvotes: 15

kindall
kindall

Reputation: 184221

Usually you will at least use the __init__() method to initialize instance variables. If you are not doing this, then by all means turn off that warning.

Upvotes: 2

Related Questions