Decorator indicating a method is both a class and an instance method
Project description
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): @combomethod def either(receiver, x, y): return x + 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 parameter been removed. But while it would then be callable from either an instance or a class, in neither case would it pass the object the method was called from. There’d never be a way to access either the class or instance variables. Ouch again!
As useful as @classmethod and @staticmethod are, they don’t handle the (occasionally important) corner 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.3, 3.4, 3.5, 3.6, and 3.7 pre-release as well as the latest PyPy and PyPy3 builds.
See CHANGES.yml for the complete Change Log.
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
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. You may also need Python-version-sepecific pip2 or pip3 installers, depending on your system configuration. In cases where pip isn’t well-configured for a specific Python instance you need, a useful fallback:
python3.6 -m pip install -U combomethod
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file combomethod-1.0.12.zip
.
File metadata
- Download URL: combomethod-1.0.12.zip
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4975b436ffe3e0a2561b58e3e9d047a50f4b16c8d4956d6a3e6ce566d034dd5e |
|
MD5 | e5c0210e774f5c64b4cc8d5b06e4a5eb |
|
BLAKE2b-256 | ae025266113d7f2057ff11a8a75018380091a1189e693c3bb9a5f62e4e8e0abc |
File details
Details for the file combomethod-1.0.12-py2.py3-none-any.whl
.
File metadata
- Download URL: combomethod-1.0.12-py2.py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e578ad7158f241cd70a9cbcfef07d6ee7a2f9b60f49b485c7ebd2477059ecc7 |
|
MD5 | c6018ca80ae4e46cdfbf8624aff4510b |
|
BLAKE2b-256 | 6e19a33293ad8405eb506849ed468f632e01e2f861f5470ed6a1a5eff7526693 |