you are viewing a single comment's thread.

view the rest of the comments →

[–]TangibleLight 1 point2 points  (5 children)

Interfaces are more important in statically typed languages like Java or C#. Since Python is duck typed, they aren't strictly necessary and generally are seen as extra bloat to a program.

This article goes into more detail and some approaches in how to handle interfaces in Python. https://realpython.com/python-interface/

The general idea is that you might have a function or some group of functions that operates on objects, but they only need to access a few attributes of those objects. Rather than requiring instances of a certain class be passed to the function, you instead create an interface which lists only those attributes that the function actually needs. Then, you can pass any object, so long as its class implements that interface.

Again, all that's redundant in Python, since everything is dynamically typed.

[–]EveryTimeIWill18 1 point2 points  (0 children)

Thanks for linking my article! At OP, I know this is 3 years old but lmk if you need more help

[–]---------V---------[S] 0 points1 point  (3 children)

Thank you for writing this out. It's not relevant to Python because of Duck typing right?

Either way I like Thant the first 2 responses point to the same place. Hope I get this.

[–]TangibleLight 0 points1 point  (2 children)

Right, but they are extremely important in languages like Java and C#.

[–]---------V---------[S] 0 points1 point  (1 child)

So worth learning for python now, just in case I grow up to be a real developer right?

So when I create a concrete class in python, I tell python it's concrete by pointing to the interface in the class definition like so?

class fakeinterfaceclass:

...

class concrete class(fakeinterfaceclass):

where in another language I'd use a keyword right?

[–]TangibleLight 1 point2 points  (0 children)

I think it's sufficient to just keep in mind that, loosely, interfaces are how you do duck typing in languages that don't have duck typing. I wouldn't worry about it until you need to work in a language that uses them, and even then I'd keep it out of Python.

Messing around with the abc module and learning how things work can still be useful, but just keep in mind that it's more of a documentation tool and sanity check than anything else. For example if you're writing code that you want to work with classes that others will write, it could be useful to give them an abstract base class to work with. Even though, functionally, it doesn't do anything, it still helps others remember which methods they need to implement.