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.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

meta_interface-0.0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: meta_interface-0.0.1.1.tar.gz
  • Upload date:
  • Size: 6.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.tar.gz
Algorithm Hash digest
SHA256 9465b22f8338498a18aa9ccb8c432872c10b690e9783632ac457de091a101b5d
MD5 5e89da856327fb0b852fd94c1b04510d
BLAKE2b-256 cd8a2cb64e788b941c4b8986bf863f1d2845a558a73ab4645da3b0b1e474d36c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for meta_interface-0.0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f265cac1127ea6d708ec126d8c982f982bfea937795e91deee8fcaa139f2138f
MD5 344b1c3be9cafb3e648a2fe0e50c1a4a
BLAKE2b-256 c71b99dfd0b219b5ede3782226ed78e16a9f39e22324767cc5b6dfd8106371e1

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