Interfaces in python
My Idea was to, in python, build something similar to java interfaces by having a parent class that raises a non implemented error if the function is not overwritten?
Yes, you're correct. Python does not support interfaces like Java. But, we can achieve a somewhat similar result using Abstract Base Classes (ABCs) in Python.
Here's a simple example:
```python
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def method_to_implement(self):
pass
```
You define a class and mark it as an `ABC` by inheriting from `ABC`. Then, any method that must be implemented in a child class can be decorated with `@abstractmethod`.
Here's how to use it:
```python
class MyClass(MyInterface):
def method_to_implement(self):
print("Implemented method")
MyClass().method_to_implement() # prints "Implemented method"
```
If you forget to implement the method, Python will raise a `TypeError`:
```python
class MyClass(MyInterface):
pass
MyClass().method_to_implement() # raises "TypeError: Can't instantiate abstract class MyClass with abstract methods method_to_implement"
```
This is about as close to a Java Interface as you can get in Python. One thing to remember though is that Python's dynamic nature allows for a lot more flexibility and there might be simpler ways to achieve your goal without the need for something as strict as interfaces in Python. The concept of "Duck Typing" in Python is typically used instead of interfaces seen in statically typed languages.
While Python doesn't support interfaces like Java, you can mimic that behavior using abstract base classes (ABCs) and the `abc` module.
Here is a brief example of how you might define a similar 'interface' in Python:
```python
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def method_to_implement(self):
pass
```
And an implementation of this 'interface':
```python
class MyClass(MyInterface):
def method_to_implement(self):
return "I've implemented this!"
```
Here, `MyInterface` works similarly to an interface. Any class that is subclassed from `MyInterface` is required to provide an implementation for `method_to_implement()`. If it doesn't, Python will raise a `TypeError`.
This mechanism of ABCs and the `abc` module in Python provide a way of ensuring certain methods are present in child classes, and thereby offers a manner of interface enforcement.
Remember that Python’s philosophy is "we're all consenting adults here". It trusts that we'll adhere to the methods outlined in the parent class where it's necessary, instead of enforcing it via the language syntax itself like Java. ABCs are there if you need some enforceability, but don't always perfectly align with Python's core design principles.
In Python, you can emulate Java-like interfaces using abstract base classes (abc module). Here is a brief summary on how you can achieve this:
```python
from abc import ABC, abstractmethod
class MyInterface(ABC):
@abstractmethod
def method_to_implement(self):
pass
```
Here, we are declaring an abstract method `method_to_implement()`. An abstract method is a method declared in an abstract class but doesn't contain any implementation. Subclasses of this abstract class are generally expected to provide an implementation for this method.
Now, any class that subclasses `MyInterface` has to implement `method_to_implement()`. If it doesn't, Python will raise a `TypeError` when you try to instantiate it.
```python
class MyClass(MyInterface):
def method_to_implement(self):
return "Implemented method"
```
But remember, Python's philosophy is "we are all consenting adults here" which means the language trusts its users not to mess things up. The concept of interfaces is contrary to Python's dynamic typing system, but if you feel a necessity to strictly enforce interface patterns, this is how you can approach it.