0

So my understanding is that methods like _privateMethod() shouldn't be usually accessed/called directly. However, due to what I need to implement, I need to override my inherited PrivateMethodClass, as:

class MyClass(PrivateMethodClass): 

   def _privateMethod(): 
        #add additional functionality 

I must extend the capabilities of this method, but my question is, is this is bad practice and if so, there any way to do this cleaner/more pythonic/better?

ocean800
  • 3,489
  • 13
  • 41
  • 73
  • 4
    If you need to override your "private" method, override it. There's not a way to do that makes it clean, other than _not_ overriding a "private" method. – khelwood Aug 02 '18 at 22:19
  • 1
    Really depends on the specific functionality ofthe method and if there are any other ways to provide that functionality. Probably you would have to override all the methods that call `_privateMethod`. – Graeme Aug 02 '18 at 22:53
  • @Graeme Thanks for the input, but why would I have to override all the methods that call `_privateMethod` as long as I keep the method signature the same? – ocean800 Aug 02 '18 at 22:55
  • @ocean800, I should correct that slightly, you could override all the non-private methods that (either directly or indirectly) call `_privateMethod`. That's one way you would avoid actually overriding the `_privateMethod`. Depending on how much work that is however, it may not be a good idea. – Graeme Aug 02 '18 at 23:02
  • @ocean800 also consider the reason for the underscore convention. It is really saying that the method may change between versions of the class/module without warning. If you are only planning to ever use one version of the class or accept that this is something you will have to change in future, overriding your `_privateMethod` is fine. – Graeme Aug 02 '18 at 23:09

0 Answers0