Skip to main content

A tiny project that introduces the interface concept to Python.

Project description

Python Metaclass Based Interface

This is a tiny project that introduces the interface concept to Python.

This documentation describes the use of Python metaclasses to create and manage interfaces in Python. The code defines two primary metaclasses, Interface and Interfaces, which can be used to enforce interface implementation and manage multiple interfaces respectively.

Usage

To use this package, it is necessary to import the package firstly. You can import partly, but it will import all here for presentation.

from interface import *

Metaclass Interface

The Interface metaclass is used to define an interface, which is a way to ensure that certain methods are implemented by any class that claims to implement the interface. This is akin to creating a contract for class behavior.

Usage

To define an interface using Interface, simply declare a class with methods decorated by @abstractmethod. Here's a basic example:

class IC(Interface):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass

Classes that want to implement this interface should use the interface as a metaclass and provide concrete implementations of all abstract methods:

class C(metaclass=IC):
    def my_abstract_method(self, ...):
        print("Implementation of abstract method")

Restricted Interface

If you want the interface to be specifically implemented by certain classes or their subclasses, the interface can be parameterized by passing a class reference to Interface. For example:

Usage

class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass

class C(Base, metaclass=IBase):
    def my_abstract_method(self, ...):
        print("Restricted implementation")

Metaclass Interfaces

The Interfaces metaclass allows a class to observe multiple interfaces. Specially, if multiple interfaces define a method with the same name, the implementation in the class observing these interfaces will apply the method from the last interface specified.

Usage

Here’s how you can use Interfaces to make a class observe multiple interfaces:

class IC(Interface):
    @abstractmethod
    def my_abstract_methodA(self, ...): pass

class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_methodB(self, ...): pass

class C(Base, metaclass=Interfaces(IC, IBase)):
    def my_abstract_methodA(self, ...):
        print('A')
    
    def my_abstract_methodB(self, ...):
        print('B')

Decorators for Interfaces

This documentation provides details on the usage of decorators abstractmethod, defaultmethod, and declaredclass in Python, designed to work with metaclasses derived from Interface. These decorators help in defining interface constraints and behaviors for classes.

Decorator abstractmethod

The abstractmethod decorator is used to mark methods as abstract within an interface. This indicates that any concrete class implementing this interface must provide an implementation for these methods.

Usage

To use abstractmethod, include it in a class that uses an Interface as its metaclass or is derived from such an interface:

class IBase(Interface[Base]):
    @abstractmethod
    def my_abstract_method(self, ...):
        pass

class C(metaclass=IBase):
    def my_abstract_method(self, ...):
        print("Implemented method")

Decorator defaultmethod

The defaultmethod decorator indicates that a method has a default implementation within an interface. Classes using this interface can override these methods, but it's not mandatory for instantiation.

Usage

Here's how to apply defaultmethod in your class definitions:

class IBase(Interface[Base]):
    @defaultmethod
    def my_method(self, ...):
        print("Default implementation")

class C(Base, metaclass=IBase):
    def my_method(self, ...):
        IBase.my_method(self, ...)  # Call the default implementation
        print("Overridden implementation")

Decorator declaredclass

The declaredclass decorator is used to indicate a base class for which an interface wil be specifically restricted. This decorator is essential when an interface is tightly coupled with a specific class.

Usage

Use declaredclass to mark a class as the base class for an interface:

@declaredclass
class Base: pass

class IBase(Interface[Base]):
    @abstractmethod
    def my_method(self, ...):
        pass

class Base(metaclass=IBase):
    def my_method(self, ...):
        print("Method implementation")

Additional Notes

  • The Interface and Interfaces metaclasses include mechanisms to ensure that classes properly implement required methods, either through direct implementation or through class hierarchy constraints.
  • The use of these metaclasses makes it clear at the class definition level which interfaces a class intends to implement, promoting better structure and organization in complex systems.
  • These decorators require that the class definition includes an appropriate metaclass, either Interface or a derivation.
  • abstractmethod and defaultmethod provide a clear indication of method requirements and implementations at the interface level, promoting more robust object-oriented design.
  • declaredclass ensures the interface is adhered to by a specific class, facilitating controlled implementations and dependencies.

This setup in Python facilitates clear, modular, and maintainable code, especially useful in large projects where enforcing design patterns and consistency is crucial.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

meta_interface-0.0.1.1.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

meta_interface-0.0.1.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file meta_interface-0.0.1.1.1.tar.gz.

File metadata

  • Download URL: meta_interface-0.0.1.1.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.0

File hashes

Hashes for meta_interface-0.0.1.1.1.tar.gz
Algorithm Hash digest
SHA256 592ea4286e63933f084a0891d521bc6e32fe45b9d156bd810a87bbb8b64333f1
MD5 6157cf92b721dc9469080df12690fc80
BLAKE2b-256 963e879d6b71b0c59dee264bae0e15432a8ba8eba913dd9d15e5c4fe87f70af7

See more details on using hashes here.

File details

Details for the file meta_interface-0.0.1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for meta_interface-0.0.1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01fdb50ea808675ebcd8e2b1f81beb9374ef5896eb484580ee57277e2a98e999
MD5 0e0a72c4caf4d135bbee6e5a35deb281
BLAKE2b-256 7d6a735f31b54af850fb87a7f0bd6bf46c60b045e52258cb27c7986aab127096

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page