3

I wonder if there is any convention regarding constructor in Python. If I have a constructor doing nothing, I can basically not writing it and everything will work just fine. However when I'm using Pycharm, it is recommending me (warning) to write an empty constructor:

__init__:
    pass

I have not find anything regarding this problem in the PEP8. I am wondering if Pycharm just come out with a new convention or if there is a reason behind that ?

Thanks.

MathiasDesch
  • 352
  • 3
  • 15
  • 2
    dont add a constructor if it does nothing ... let it use the default constructor that also does nothing ... dont worry about pycharms warning – Joran Beasley Feb 12 '15 at 17:03
  • Thanks. I do agree about this point of view and that's what I'm doing. But since IntelIJ are pretty serious IDEs, I was wondering if I missed a convention – MathiasDesch Feb 12 '15 at 17:06
  • If that is part of pep-8 I have never heard of it ... and tbh it probably needs to be changed to recommend not doing this – Joran Beasley Feb 12 '15 at 17:07
  • 1
    First of all, if you were to do something like that, it would need to be `def __init__(self):` then `pass` -- so that's recommendation is bogus. Second of all, doing it (properly) will prevent the constructor of your base class from executing, so it's not equivalent to leaving it out (in which case it will be executed by default). – martineau Feb 12 '15 at 18:38

4 Answers4

4

I think it's opinion based, but I will share rules that I try to follow: 1. Declare all instance variables in constructor, even if they are not set yet

def __init__(self, name):
    self.name = name
    self.lname = None
  1. Do not do any logic in the constructor. You will benefit from this when will try to write unittests.

  2. And of course if it's not necessary dont' add it.

Vor
  • 33,215
  • 43
  • 135
  • 193
  • great answer to a question that really shouldnt be here ... but you should link to supporting documentation of these rules (again I agree with you ... but right now this is just "because I think this is the right way") which is not technically a valid answer (I still gave you +1) – Joran Beasley Feb 12 '15 at 17:12
  • @Joran Beasley thanks, for coaching on how to improve the answers, will try to back up it with some links. – Vor Feb 12 '15 at 17:14
  • Test classes don't and shouldn't use __init__, and this warning isn't about that. This shoulnd't be the accepted answer as it doesn't actually answers the question. – aimbire Sep 18 '17 at 21:57
3

I agree with the sentiment to not write unnecessary code. The warning is probably there to help speed up development since most classes probably have an init and this will remind you to write it ahead of time.

It is possible to customize or suppress this warning in Settings -> Editor -> Inspections -> Python -> "Class has no __init__ method"

Brian Mego
  • 1,439
  • 11
  • 10
1

Don't add a constructor if it doesn't do anything. Some editors like to warn you for some silly things. Eclipse for example, warns you when variables are initialized but not used later on or when classes don't have a serializable id. But that's Java. If your program will run without it, then remove the constructor.

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
1

Class has no __init__ method

I keep seeing this when I make a tiny, ad hoc class. I use this warning as a cue:

I forgot to derive the class from something sensible.

Exception Object

For example, an exception class that I would use to raise CustomException.

No:

class CustomException:
    pass

Yes:

class CustomException(Exception):
    pass

Because Exception defines an __init__ method, this change makes the warning go away.

New Style Object

Another example, a tiny class that doesn't need any member variables. The new style object (explicit in Python 2, implicit in Python 3) is derived from class object.

class SettClass(object)
    pass

Similarly, the warning goes away because object has an __init__ method.

Old Style Object

Or just disable the warning:

# noinspection PyClassHasNoInit
class StubbornClass:
    pass
Community
  • 1
  • 1
Bob Stein
  • 16,271
  • 10
  • 88
  • 101