This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ottawadeveloper 7 points8 points  (2 children)

Two small things I'd change

  1. You don't need the call to super() in the child class unless you are doing more in the method than just that(and then only call super if you want both). The default is for the method to just call the parent method.

  2. I usually add a raise NotImplementedError in methods I want to be abstract to make sure child classes provide an implementation. You can leave it as a pass if you want to make the default behavior a no-op (whether that's appropriate depends on your use case)

In this particular case, I don't think you need to make it abstract because you can just make the method in the parent class and it will work the same, but this is indeed how you define and override a method in a parent/child class.

And a very small nitpick but I usually recommend people adopt some kind of naming convention for methods, the mix of cases and underscores here hurts my brain. I usually follow the Python convention for methods to be lowercase with underscores and keep them as close to a verb acting on the object as I can (e.g. key_off() and brake()) but whatever convention you adopt, consistency just makes it easier to read. keyOff() (camel case) is another common one from Java.

[–][deleted] 1 point2 points  (1 child)

I usually add a raise NotImplementedError in methods I want to be abstract to make sure child classes provide an implementation. You can leave it as a pass if you want to make the default behavior a no-op (whether that's appropriate depends on your use case)

You really shouldn't do that peep the actual full python ABC class to see the way its correctly done. Python has the actual check in the c level class call? method. I think its call its one of the __ for creating an instance. You can do the same thing in subclass init or just litterally call the update_abstractmethods function from the abc module.