Skip to main content

Decorator indicating a method is both a class and an instance method

Project description

travisci PyPI Package latest release PyPI Package monthly downloads Supported versions Supported implementations Wheel packaging support Test line coverage Test branch coverage

Python has instance methods, class methods (@classmethod), and static methods (@staticmethod). But it doesn’t have a clear way to invoke a method on either a class or its instances. With combomethod, it does.:

from combomethod import combomethod

class A(object):

    base = 10

    def __init__(self, base=100)

    @combomethod
    def either(receiver, x):
        return receiver.base + y

a = A()
assert a.either(1, 3) == 4
assert A.either(1, 3) == 4

Voila! You method now takes either the class or the instance–whichever one you want to call it with.

Discussion

In some cases, you can fake @combomethod with @classmethod. In the code above, for example, there is no real reference to the class or instance, and either could have been designated a @classmethod, since they can be called with either classes or instances. But, there’s a problem: Class methods always pass the class to the method, even if they’re called with an instance. With this approach, you can never access the instance variables. Ouch!

Alternatively, either could have been designated a @staticmethod, had its receiver variable been removed. Then it would be callable from either an instance or a class. But in neither case would it pass the object the method was called from, so there’d never be a way to access either the class or instance variables. Double ouch!

So as useful as @classmethod and @staticmethod are, they don’t handle the important case where you need to call with either the class or an instance and you need genuine access to the object doing the call. Here’s an example that needs this:

class Above(object):

    base = 10

    def __init__(self, base=100):
        self.base = base

    @combomethod
    def above_base(receiver, x):
        return receiver.base + x

a = Above()
assert a.above_base(5) == 105
assert Above.above_base(5) == 15

aa = Above(12)
assert aa.above_base(5) == 17
assert Above.above_base(5) == 15

When you need to call with either an instance or a class, and you also care about the object doing the calling, @combomethod rocks and rolls.

Notes

  • This module is primarily a convenient packaging, testing, and documentation of insights and code from Mike Axiak’s Stack Overflow post. Thank you, Mike!

  • Automated multi-version testing managed with pytest, pytest-cov, coverage, and tox. Continuous integration testing with Travis-CI. Packaging linting with pyroma.

    Successfully packaged for, and tested against, all late-model versions of Python: 2.6, 2.7, 3.2, 3.3, 3.4, and 3.5 pre-release (3.5.0b3) as well as PyPy 2.6.0 (based on 2.7.9) and PyPy3 2.4.0 (based on 3.2.5).

  • The author, Jonathan Eunice or @jeunice on Twitter welcomes your comments and suggestions.

Installation

To install or upgrade to the latest version:

pip install -U combomethod

To easy_install under a specific Python version (3.3 in this example):

python3.3 -m easy_install --upgrade combomethod

(You may need to prefix these with sudo to authorize installation. In environments without super-user privileges, you may want to use pip’s --user option, to install only for a single user, rather than system-wide.)

Project details


Download files

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

Source Distributions

combomethod-1.0.0.zip (12.7 kB view details)

Uploaded Source

combomethod-1.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

combomethod-1.0.0-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file combomethod-1.0.0.zip.

File metadata

  • Download URL: combomethod-1.0.0.zip
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for combomethod-1.0.0.zip
Algorithm Hash digest
SHA256 1883bcbffb7be45e43cc535521a599f9a66ce048153e5c7958fc180a94d8449b
MD5 bc808f900bb87cfd7bf7dc5b7691f997
BLAKE2b-256 362679f441fc77481c9d3f634452fef112c9fb950e11721734401f01c63e97f9

See more details on using hashes here.

File details

Details for the file combomethod-1.0.0.tar.gz.

File metadata

  • Download URL: combomethod-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for combomethod-1.0.0.tar.gz
Algorithm Hash digest
SHA256 93574dde4ef6e8c8c7a4c2ffe4d1ea09c0c0a525d1a03f47590da068ae72079d
MD5 e06f88318f8175db8b5f8f2adf79b785
BLAKE2b-256 a19e74d3d817076074fd9f27a63f904277ece0f97f86f974998c036417ae0b04

See more details on using hashes here.

File details

Details for the file combomethod-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for combomethod-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ceee246b2488b86fda341d04eb099a1e32cd5923317527d6d5c27f2acd2f3ce
MD5 d4d7447643ae0cfd67d2cb57b4b359e0
BLAKE2b-256 f30b0dda8629bf61b5f7ebdfa34c812cb5ec1482f8500f401932865820505e2a

See more details on using hashes here.

Supported by

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