Decorator to prevent base class methods from being called directly.
Project description
Decorator to prevent base class methods from being called directly.
Usage
from virtualmethod import virtualmethod class Base(object): @virtualmethod def my_virtual_method(self): print "This is a virtual method. Call me from a subclass!" class A(Base): pass class B(Base): def my_virtual_method(self): print "I went ahead and implemented my own version." base = Base() a = A() b = B() a.my_virtual_method() -> This is a virtual method. Call me from a subclass! b.my_virtual_method() -> I went ahead and implemented my own version. base.my_virtual_method() -> Traceback (most recent call last): File "<stdin>", line 1, in <module> File "virtualmethod/core.py", line 11, in __call__ raise TypeError("Virtual method %s must be called from a subclass." % self.__name__) TypeError: Virtual method my_virtual_method must be called from a subclass.
Note(s)
@virtualmethod works with @classmethod and @staticmethod, but must be declared first, i.e.:
@virtualmethod @classmethod def my_virtual_class_method(cls): ...
Credit
Thanks to Patrick Hensley for his input, and to Denis Ryzhkov for his method_decorator module.
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
virtualmethod-0.0.1.tar.gz
(3.5 kB
view details)
File details
Details for the file virtualmethod-0.0.1.tar.gz
.
File metadata
- Download URL: virtualmethod-0.0.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4735ca7ee36b7b858ee5c71fd53123e599375db1f11eb16f60adcc5ce55feac2 |
|
MD5 | ee70c31128e67bedfce2698c355b4ee4 |
|
BLAKE2b-256 | 96dab1e61548b584947445311290de9b45604dc7ef0374cc148b276501b3d7e5 |