Skip to main content

Metaclass enabling protected mecanism on method with the Protected decorator

Project description

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

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

protected_method_metaclass-1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file protected_method_metaclass-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for protected_method_metaclass-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a4237b04d80d7c083e70c2a0aedaf4653fe5e971d2c477505d07cbcfdb1000d
MD5 1ce523fabaa7454b9b1987f4237d6873
BLAKE2b-256 3e79f12c43655f315a686636d02b0e061b321a49751d852d1a5f9eb465e25728

See more details on using hashes here.

Supported by

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