Skip to main content

Dynamic class and instance methods!

This simple module creates a class method that will also work as an instance method.

import dynamicmethod

class Example(object):

    x = 1  # Default classmethod value

    def __init__(self, x=0):
        self.x = x

    @dynamicmethod
    def get_x(self):
        return self.x

print(Example.get_x())
ex = Example()
print(ex.get_x())
ex.x = 5
print(ex.get_x())

I have grown to use this module quite a bit. Mostly I make a default class level dictionary where each instance can be customized.

from dynamicmethod import dynamicmethod

class Lookup(object):

    lookup_type = {'type1': 1, 'mytype2': 2}

    def __init__(self):
        # Save an instance variable so the class variable does not change.
        self.lookup_type = self.__class__.lookup_type.copy()

    @dynamicmethod
    def add_type(self, name, value):
        self.lookup_type[name] = value

    @dynamicmethod
    def get_type(self, name):
        return self.lookup_type.get(name, None)

Lookup.add_type('New Type', 3)
assert 'New Type' in Lookup.lookup_type
print(Lookup.get_type('New Type'))

specific_lookup = Lookup()
assert 'New Type' in specific_lookup.lookup_type
print(specific_lookup.get_type('New Type'))

specific_lookup.add_type('hello', 'world!')
assert 'hello' in specific_lookup.lookup_type
assert 'hello' not in Lookup.lookup_type
print(specific_lookup.get_type('hello'))
# print(Lookup.get_type('hello'))  # This would be None since the class lookup does not contain 'hello'

# Check adding to general after an instance is created
Lookup.add_type('Fresh Type', 4)
assert 'Fresh Type' in Lookup.lookup_type
assert 'Fresh Type' not in specific_lookup.lookup_type

new_lookup = Lookup()
assert 'Fresh Type' in new_lookup.lookup_type

Metadata

Release files for dynamicmethod 1.1.0

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

Source distribution (sdist)

Source distribution for dynamicmethod 1.1.0
File Size Uploaded Provenance
dynamicmethod-1.1.0.tar.gz 4.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dynamicmethod 1.1.0
File Interpreter ABI Platform
dynamicmethod-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / dynamicmethod-1.1.0.tar.gz

Download URL dynamicmethod-1.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4f2d8b257d90d18e58e971759e424cb8c368509adc1f042f7c7601b2770beac7
BLAKE2b-256 checksum
How to use checksums
e3346ec29c214d7ee9a470f0d89274af0e8d8b9d31448802a93cf4c71e982a13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

Release files / dynamicmethod-1.1.0-py3-none-any.whl

Download URL dynamicmethod-1.1.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
378209e664016a999bcba632dbaa9e09e6c6ae827d08669f795b6487d5ac58e2
BLAKE2b-256 checksum
How to use checksums
1751fa6ca936d11e1b13402b4eb2d0475785ac2c3dde630ca6fbc60f3596dcfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

Security for dynamicmethod 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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