Python Forum
Right way to implement interfaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right way to implement interfaces
#1
Hi all!

There is a right way to implement interfaces?
I read about ABC but I heard it going to be deprecated so I want to know if there is other way for it.

Thanks!
Reply
#2
Where have you read that ABCs are going to be deprecated? The only news I read about this is that importing some ABCs from module collections is deprecated, for example
from collections import Sequence
is deprecated and one should use
from collections. abc import Sequence
yossiy123 Wrote:There is a right way to implement interfaces?
This question suggests that you may come from a Java background or a similar language where interfaces play a major role. This is not so in Python and interfaces are often just a gentlemen's agreement that a class will possess such and such method that behave in a certain way. Python is much more informal.

For example the Iteration protocol supposes that a class must have an __iter__ method that returns an iterable which in turn have an __iter__ and a __next__ method with appropriate semantics. Classes that implement this protocol don't need to declare explicitly that they implement the iteration protocol and that is the way many of these classes work. These classes can also declare that they are «subclasses» of the ABCs collections.abc.Iterable and collections.abc.Iterator respectively.

As far as I know, Abstract Base Classes are the still correct way to have your class explicitly declare that they implement an interface.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a function to get IP addresses of interfaces Skaperen 2 1,449 May-30-2022, 05:00 PM
Last Post: Skaperen
Question best way to implement algorithm hamidze 3 2,233 Feb-27-2021, 07:10 PM
Last Post: hamidze

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020