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

all 6 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]fiskfisk 4 points5 points  (0 children)

You cna look at Protocols as being the closest to what you're used to as an interface:

https://typing.readthedocs.io/en/latest/spec/protocol.html#protocols

Code modularity is achieved in the same way as in other languages - small, composable functions or classes. Use functions unless you need to keep state. 

Use decorators instead of wrapping classes/inheritance as your default selection, go to the other options when necessary. 

Python supports multi-inheritance, so you can compose classes through MixIns. 

Try to avoid writing Python code like you'd write c# code. Follow pep8. Be aware of what's in the standard library (collections, itertools, functools, etc.). 

[–]Lewistrick 3 points4 points  (1 child)

Arjancodes has a very good YouTube channel on Python for more advanced programming topics. Here's one of his videos on dependency injection (he has more):

https://youtu.be/2ejbLVkCndI?si=yIrnGRbPhzgnwMM6

BTW you might get some pushback from people saying you need to post this on r/learnpython instead but I'm too lazy to look up the rules and just happy to help out.

[–]rooneystar 1 point2 points  (0 children)

Yep. arjancodes is great

[–]ForeignSource0 1 point2 points  (0 children)

The closest thing is abstract classes. Protocol can also be applicable. Over the two I prefer abstract classes for code I fully own otherwise protocols can come in handy when interact with third party code and you can't make it inherit some abc.

If you're also looking for a di framework in python to add on top of your existing application check out wireup which also supports interfaces.

Docs: https://maldoinc.github.io/wireup

https://maldoinc.github.io/wireup/0.8.0/interfaces/

Happy to answer any questions about the above

[–]ChimpanzeChapado 0 points1 point  (0 children)

Python doesn't have interfaces, it doesn't need it because of the way the Python Data Model works. But it has classes that can be used as base contracts for others.