devctrl.utility.decorators.ClassOrInstanceMethod#

class ClassOrInstanceMethod(f)[source]#

Bases: object

Decorator to make a class member function a class or instance method, depending on how it is called.

The first argument will always be the class (usually called cls for class methods). If the method is called from an instance, the instance will be added as keyword argument ‘instance’.

Example:

class Test:
def __init__(self, name):

self.name = name

@ClassOrInstanceMethod def test(cls, instance=None):

if instance is None:

print(“Called from class”)

else:

print(f”Called from instance {instance.name}”)

t = Test(“#1”) t.test() Test.test()

Methods

__annotations__ = {}#
__dict__ = mappingproxy({'__module__': 'devctrl.utility.decorators', '__firstlineno__': 1, '__doc__': 'Decorator to make a class member function a class or instance method, depending on how it is called.\n\nThe first argument will always be the class (usually called cls for class methods). \nIf the method is called from an instance, the instance will be added as keyword argument \'instance\'.\n\nExample:\n\n    class Test:\n        def __init__(self, name):\n            self.name = name\n\n        @ClassOrInstanceMethod\n        def test(cls, instance=None):\n            if instance is None:\n                print("Called from class")\n            else:\n                print(f"Called from instance {instance.name}")\n\n    t = Test("#1")\n    t.test()\n    Test.test()\n            \n', '__init__': <function ClassOrInstanceMethod.__init__>, '__get__': <function ClassOrInstanceMethod.__get__>, '__static_attributes__': ('f',), '__dict__': <attribute '__dict__' of 'ClassOrInstanceMethod' objects>, '__weakref__': <attribute '__weakref__' of 'ClassOrInstanceMethod' objects>, '__annotations__': {}})#
__firstlineno__ = 1#
__get__(obj, klass=None)[source]#
__init__(f)[source]#
__module__ = 'devctrl.utility.decorators'#
__static_attributes__ = ('f',)#
__weakref__#

list of weak references to the object