Introduction to Accessify
Accessify is a Python library that allows developers to control access to class attributes and methods. It brings the power of encapsulation in object-oriented programming, enabling strictly defined access levels similar to those in languages like Java and C++. With Accessify, you can set boundaries on how parts of a program can interact, leading to more secure and robust code.
API Explanations and Code Snippets
Public
Public members are accessible from anywhere, both inside and outside the class or module.
from accessify import public class MyClass: @public def my_public_method(self): return "This is a public method" obj = MyClass() print(obj.my_public_method())
Protected
Protected members are accessible within the class and its subclasses.
from accessify import protected class MyClass: @protected def my_protected_method(self): return "This is a protected method" class SubClass(MyClass): def access_protected(self): return self.my_protected_method() obj = SubClass() print(obj.access_protected())
Private
Private members are accessible only within the class where they are defined.
from accessify import private class MyClass: @private def my_private_method(self): return "This is a private method" def access_private(self): return self.my_private_method() obj = MyClass() print(obj.access_private())
Constants
Defining constants
from accessify import private class MyClass: CONSTANT_NAME = "This is a constant" @private def get_constant(self): return MyClass.CONSTANT_NAME obj = MyClass() print(obj.get_constant())
Final
Marking methods as final to prevent overriding in subclasses.
from accessify import private, override class BaseClass: @private def method_to_be_private(self): return "This method is private in BaseClass" class ChildClass(BaseClass): @override def method_to_be_private(self): return "This method will not be called" print(ChildClass().method_to_be_private())
App Example Using Introduced APIs
from accessify import public, protected, private class BankAccount: def __init__(self, balance): self._balance = balance @public def get_balance(self): return self._balance @protected def set_balance(self, amount): if amount >= 0: self._balance = amount @private def _calculate_interest(self): return self._balance * 0.05 def apply_interest(self): interest = self._calculate_interest() self.set_balance(self._balance + interest) account = BankAccount(1000) print(account.get_balance()) account.apply_interest() print(account.get_balance())
With Accessify, you can define clear boundaries in your code to control access, leading to better maintainability and reduced risk of unintended interactions.
Hash: e615b70fe1f72cbf9c086370e3a16360023211d19f12887924458c4438dffe46