I have a specific problem closely related to PyCharm (Community 3.1.1). The following simple example illustrates this. I will use the screenshot of PyCharm rather than type the code, for reasons that will be clear shortly.
As you can see, the call to self.say_hello()
is highlighted in yellow by PyCharm, and presumably this is because say_hello()
is not implemented in the Base
class. The fact that say_hello()
is not implemented in the base class is intentional on my part, because I want a kind of "abstract" effect, so that an instance of Base
cannot call say_hello()
(and therefore shouldn't call hello()
), but that an instance of Child
can call hello()
(implemented in the Base
class). How do I get this "abstract" effect without PyCharm complaining?
As I learned from here, I could use the abc
module. But that, to me, would be rather cumbersome and somewhat not pythonic. What are your recommendations?
I would implement say_hello()
as a stub:
class Base(object):
# ...as above...
def say_hello(self):
raise NotImplementedError
Alternatively, put only pass in the body of say_hello()
.
This would also signal to the user of your Base
class that say_hello()
should be implemented before she gets an AttributeError when calling obj.hello()
.
Whether to raise an Exception or to pass
depends on whether doing nothing is sensible default behaviour. If you require the user to supply her own method, raise an exception.
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