touketsu.core.urt_method

@touketsu.core.urt_method

Decorate instance methods to temporarily allow attribute creation.

Apply urt_method() to instance methods to temporarily remove the class restriction during the execution of the instance method. For example, suppose we have a class a_class, where

from touketsu import nondynamic, urt_method

@nondynamic
class a_class:

    def __init__(self, a = "a"):
        self.a = a

    @classmethod
    def a_class_method(cls):
        cls.aa = 1000
        return cls(a = "A")

    @urt_method
    def method_one(self, val):
        self.b = val

    def method_two(self):
        if hasattr(self, "b"):
            return f"{self.a} UwU {self.b}"
        return f"{self.a} T^T"

method_one needs to be decorated with urt_method() since it will create an instance attribute b when called. However, method_two does not need to be decorated since it is not creating or modifying any instance attributes. The classmethod() a_class_method also does not need to be decorated since touketsu restrictions only apply to class instances, not the classes themselves.

Parameters

meth (function) – An unbound instance method

Returns

A decorated unbound instance method that allows instance attribute modification and creation during its execution.

Return type

function