Skip to main content

virtualmethod

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.

Metadata

Release files for virtualmethod 0.0.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 virtualmethod 0.0.1
File Size Uploaded Provenance Actions
virtualmethod-0.0.1.tar.gz 3.5 kB Without provenance Details for virtualmethod-0.0.1.tar.gz

Release files / virtualmethod-0.0.1.tar.gz

Download URL virtualmethod-0.0.1.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4735ca7ee36b7b858ee5c71fd53123e599375db1f11eb16f60adcc5ce55feac2
BLAKE2b-256 checksum
How to use checksums
96dab1e61548b584947445311290de9b45604dc7ef0374cc148b276501b3d7e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for virtualmethod 0.0.1

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.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

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