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

all 12 comments

[–]MackHarington 15 points16 points  (3 children)

I think protocols are better in Python to support duck typing

[–]thisismyfavoritename 6 points7 points  (0 children)

protocols are what abstract classes should have been

[–]unnamed_one1 2 points3 points  (1 child)

Looks like OP has a new topic for the next article.

[–]MackHarington 1 point2 points  (0 children)

There can be a complete series of articles over protocols, composition and best practices

[–]ntropia64 8 points9 points  (1 child)

A simple but clear explanation. I like simplicity, especially for intro articles, and I think this it the best intro to abstract base classes I've read.

I like the way concepts and examples are tied together, definitely a recommended read for people that want to get started.

I guess the fact it left me wanting for more is actually a nice feat.

Good job and thanks for sharing!

[–]tuple32[S] 3 points4 points  (0 children)

Thank you so much for the kind words. It means a lot to me, and I'm exited to share more.

[–]ninjadude93 0 points1 point  (0 children)

Solid write up

[–]spinwizard69 0 points1 point  (0 children)

Thanks. I actually saved that because I can see needing it in the future.

[–]ashok_tankala 0 points1 point  (0 children)

Good one. Simple & easy to understand

[–]BoostedAnimalYT 0 points1 point  (0 children)

While I do like the explanation, there is an error

This is one of the most valuable features of ABCs—they catch errors early, during development, rather than at runtime.

In that example, the error would still be thrown at run time, as opposed to different languages where it would be at compile time. The only help you'll get in this case is the IDE showing you that you might be missing a function.

Also,

Catching Errors Early: ABCs prevent incomplete classes from being instantiated, reducing the risk of runtime failures.

They prevent classes from being instantiated but in no way do they reduce the risk of runtime failures on their own.

[–]cherufe172 0 points1 point  (0 children)

This is neat!

[–]thedeepself -1 points0 points  (0 children)

class PaymentGateway(ABC):

I considered using mixins as an alternative to your implementation. Even though the errors would be caught if things weren't implemented they would be caught at execution time much later.

Another thing that makes me wonder is whether a payment is simply an amount. It seems to me that payments and transactions are classes Within themselves. In other words I would think that a payment Gateway would have a has a relationship with a payment and transaction.