Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Private Method Decorator

def private_method(method):
    def wrapper(self, *args, **kwargs):
        if not isinstance(self, method.__qualname__.split('.')[0]):
            raise AttributeError(f"{method.__name__} cannot be called outside of its class.")
        return method(self, *args, **kwargs)
    return wrapper

class MyClass:
    @private_method
    def my_private_method(self, arg1, arg2):
        return arg1 + arg2

# This will work fine
obj = MyClass()
result = obj.my_private_method(3, 4)
print(result)  # Output: 7

# This will raise an AttributeError
try:
    MyClass.my_private_method(3, 4)
except AttributeError as e:
    print(e)  # Output: my_private_method cannot be called outside of its class.

About

Some Templates

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors