Skip to main content

The antonym of super(): let a base class hijack a derived class's overridden method

Project description

📟🔃 Unter

PyPI Licence

A Python module of decorators to allow base classes to selectively enforce their primacy even in the face of derived classes overriding implementations.

Overview

The unter module provides a set of decorators that can be applied to the base classes and its methods in order to ensure that calls of a method overridden in a derived class nevertheless invoke the base class implementation instead.

Normal Python method resolution order resolves the most-overridden method first when a call is made, and the ability for base classes to provide a common frame around an implementation customisable by derived classes is only achievable through either the explicit use of super(), or via template functions.

With unter, the base class can break the normal method resolution order and enforce its own implementation to be called first, while leaving the derived class to visually appear as-if to override the base class method (instead of _*_impl-style methods).

Installation

pip install unter

Quick-start

Current, Pythonic way of doing things

class Base:
    def method(self):
        print("Base.method() called")
        self._method()  # Calls the derived class method
        print("Base.method() over!")

    def _method(self):
        print("Base._method() - not overridden")

class Derived(Base):
    def _method(self):
        print("Derived._method() - overridden")

    # Whoops, should never do this by accident, otherwise, the implementation
    # would break!
    # def method(self):
    #     ...


>>> Base().method()
Base.method() called
Base._method() - not overridden
Base.method() over!

>>> Derived().method()
Base.method() called
Derived._method() - overridden
Base.method() over!

Basic decoration and primacy

from unter import unter

@unter.enable
class Base:
    @unter.attorney
    def method(self) -> int:
        print("Base.method() called")
        i = 1 + unter().method()
        print("Base.method() over!", i)
        return i

class Derived:
    def method(self) -> int:
        print("Derived.method() called")
        i = 4
        print("Derived.method() over!", i)
        return i

>>> print(Derived().method())
Base.method() called
Derived.method() called
Derived.method() over! 4
Base.method() over! 5
5

Why?

unter is a highly specialised tool to invert the normal method resolution behaviour of class hierarchies. Specialised tools are things that one needs rarely, but when they are needed, they are needed badly.

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

unter-0.1.0a1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unter-0.1.0a1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file unter-0.1.0a1.tar.gz.

File metadata

  • Download URL: unter-0.1.0a1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for unter-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 35a053c68a1d9aff9dcba6aa84bb3378deaa876c994c01b5163bc6ccbe70122e
MD5 c0497eebfff6ed7a5b4b9123b17bd214
BLAKE2b-256 22e4058c6545bf3da9f3433f5eafa1663c20fbcbe5fd378705a12227ddfa66e4

See more details on using hashes here.

File details

Details for the file unter-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: unter-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for unter-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc99a84b38e01e703f5dc7b96b6e04406f9f0c94827df7483ebac9ed1f4ed31e
MD5 ccdddb01d69fb2420630da98389e80c5
BLAKE2b-256 8b0ca2d3f6e7fddb7715fafb74096e730fd15cd8aa06b77152e8ff74ec272b90

See more details on using hashes here.

Supported by

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