Skip to main content

protected_method_metaclass - Metaclass enabling the protected feature on methods

This package contains several metaclasses that allow the protected feature on the applied class and this all the subclasses.
The mechanism should works with multiple inheritance but you should still be carefull in this case.

When overloading a protected method, the overloaded method can still be accessed with the '__old' prefix (see later)

Simple example

Creating a base class

from protected_method_metaclass import ProtectedMethodMetaClass, Protected

# ==========================================================
class BaseClass(object, metaclass=ProtectedMethodMetaClass):  # The base class must use the ProtectedMethodMetaClass metaclass

    # ====================
    def __init__(self, a):
        self.__a = a

    # ==============
    def get_a(self):
        return self.__a

    # ========
    @Protected  # Use the Protected decorator on private methods you want to make protected
    def __set_a(self, a):
        print("IN BASE CLASS")
        self.__a = a

Use with sub class and overloading

# ==========================
class SubClass_1(BaseClass):
    # ===================
    def __set_a(self, a):  # You don't need to repeat the Protected decorator to overload
        print("IN SUB CLASS 1")
        self.__old_set_a(a)  # use the '__old' prefix for the overloaded method

    # =============================
    def call_former_set_a(self, a):
        self.__old_set_a(a)

    # ==========================
    def call_new_set_a(self, a):
        self.__set_a(a)

    # ========
    @Protected  # You an declare protected method in any subclass of a class using the ProtectedMethodMetaClass metaclass
    def __new_protected_method(self):
        print("new_protected_method")

the_instance = SubClass_1("a")
the_instance.call_former_set_a("a1)
>>> IN BASE CLASS
the_instance.get_a()
>>> a1

the_instance.call_new_set_a("a2")
>>> IN SUB CLASS 1
>>> IN BASE CLASS
the_instance.get_a()
>>> a2

Multiple sub classes

The following example details the behaviour of a protected method and of the '__old' prefix when there is a hierarchy of sub classes.

# ===========================
class SubClass_2(SubClass_1):
    # ===================
    def __set_a(self, a):
        print("IN SUB CLASS 2")
        self.__old_set_a(a)

the_instance = SubClass_2("a")
the_instance.call_former_set_a("a1)  # The behaviour is not modified': __old_set_a' used in SubClass_1 still refers '__set_a' of BaseClass
>>> IN BASE CLASS 
the_instance.get_a()
>>> a1

the_instance.call_new_set_a("a2")  # The behaviours is modified as '__set_a' always points to the latest overloaded method
>>> IN SUB CLASS 2
>>> IN SUB CLASS 1
>>> IN BASE CLASS
the_instance.get_a()
>>> a2

PyQt5 and PySide2

As most classes from PyQt or PySide2 use their own metaclass, the standard ProtectedMethodMetaClass is not directly usable for Qt classes.
The package protected_method_metaclass contains two other metaclasses compatible with PyQt5 or PySide2. They are designed to be used exactly as the native ProtectedMethodMetaClass with one for PyQt5 and one for PySide2.
Yet, you do not need to have any of those library installed as a requirement for the protected_method_metalass package. The matching metaclasses will be available only if the Qt libraries are installed and disabled otherwise.

Example with PyQt5

from protected_method_metaclass import ProtectedMethodMetaClass_Qt
from PyQt5.QtWidgets import QWidget

class MyBaseWidget(QWidget, metaclass=ProtectedMethodMetaClass_Qt):
    pass

Example with PySide2

from protected_method_metaclass import ProtectedMethodMetaClass_PySide2
from PySide2.QtWidgets import QWidget

class MyBaseWidget(QWidget, metaclass=ProtectedMethodMetaClass_PySide2):
    pass

Release files for protected-method-metaclass 1.0

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

Built distribution (wheel)

Table of built distributions (wheels) for protected-method-metaclass 1.0
File Interpreter ABI Platform
protected_method_metaclass-1.0-py3-none-any.whl Python 3 none any Details

Release files / protected_method_metaclass-1.0-py3-none-any.whl

Download URL protected_method_metaclass-1.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a4237b04d80d7c083e70c2a0aedaf4653fe5e971d2c477505d07cbcfdb1000d
BLAKE2b-256 checksum
How to use checksums
3e79f12c43655f315a686636d02b0e061b321a49751d852d1a5f9eb465e25728
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

1.0 This release

1 release file

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