Skip to main content

Implements the impl pattern*

/* inspired by Rust

Useful when it is needed to extend a class (usually 3d party) with some methods

Install

pip install impl_pattern

or

poetry add impl_pattern

Usage

Regular methods

from impl_pattern import impl

class Sample:
    def __init__(self):
        self.value = 10

@impl(Sample)
def plus_one(self: Sample):
    self.value += 1

s = Sample()
s.plus_one()

print(s.value) 
# 11

it works with async methods as well

from asyncio import sleep

@impl(Sample)
async def plus_one(self: Sample):
    await sleep(1)
    self.value += 1

s = Sample()
await s.plus_one()

print(s.value) 
# 11

Class methods

To register function as a classmethod you can use impl_classmethod decorator

from impl_pattern import impl_classmethod

class Sample:
    value = 10

@impl_classmethod(Sample)
def plus_one(cls):
    cls.value += 1

Sample.plus_one()

print(Sample.value) 
# 11

This works with async methods too

from asyncio import sleep

@impl_classmethod(Sample)
async def plus_one(cls):
    await sleep(1)
    self.value += 1

await Sample.plus_one()

print(Sample.value) 
# 11

Static methods

Static methods use the same syntax but with the impl_staticmethod decorator

from impl_pattern import impl_staticmethod

class Sample:
    ...

@impl_staticmethod(Sample)
def get_one():
    return 1

print(Sample.get_one()) 
# 1

This works with async methods too

from asyncio import sleep

@impl_staticmethod(Sample)
async def get_one():
    return 1

print(await Sample.get_one()) 
# 1

Metadata

Release files for impl-pattern 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for impl-pattern 0.1.1
File Size Uploaded
impl_pattern-0.1.1.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for impl-pattern 0.1.1
File Interpreter ABI Platform
impl_pattern-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / impl_pattern-0.1.1.tar.gz

Download URL impl_pattern-0.1.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b4e773a16335fc6653b5a787ef47fc16eb041f334809e3507d8f19153d87f674
BLAKE2b-256 checksum
How to use checksums
35ade4b66ce306f06f5a0b92a8e3e93fe76abaed5991feb0152823c5fa2736b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

Release files / impl_pattern-0.1.1-py3-none-any.whl

Download URL impl_pattern-0.1.1-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d5518f335260c7f858558e47029bc489205d960a44c8abde28495592a6ddf55
BLAKE2b-256 checksum
How to use checksums
113438f8b291141ea191855228a2c6b5d5d53fb928459864d037748d7a4c8017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.8 CPython/3.9.5 Linux/5.11.0-46-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page