Skip to main content

Python Kotlin Acess Modifier

Project description

Python Access Modifiers Library

This library provides access control mechanisms for Python classes, including Private, Protected, Internal, and Public variables and methods. Python does not enforce strict access modifiers like Java or C++, but this library uses descriptors and decorators to simulate them.

How to install

pip install PyAccessModifier

or

pip install git+https://github.com/howShouldIChooseMyUsername/PyAccessModifier.git

Setup

from PyAccessModifier import *  # Recommended
# or
import PyAccessModifier

Variable Descriptors

1. Private

  • Purpose: Restrict access to the variable only within the defining class.
  • Example:
@API
class MyClass:
    @privateinit
    def init(self):
        self._privateValue = Private(40)
obj = MyClass()
print(obj.my_private)  # PermissionError
  • Behavior:
    • Reading or writing from outside the class raises PermissionError.
    • Only instances of the defining class can access the value.

1.1. Private (Class-level)

  • Purpose: Restrict access to the class-level variable only within the defining class.
  • Example:
from PyAccessModifier import Private, public


class MyClass:
    # Class-level private variable
    class_private = Private(42)

    @public
    def show_private(self):
        print("Internal access:", self.class_private)

    @public
    def change_private(self, value):
        self.class_private = value


obj = MyClass()

# Access via class methods (allowed)
obj.show_private()  # Internal access: 42
obj.change_private(99)
obj.show_private()  # Internal access: 99

# Direct external access (raises PermissionError)
print(obj.class_private)  # PermissionError
obj.class_private = 123  # PermissionError
  • Behavior:
    • Reading or writing from outside the class raises PermissionError.
    • Accessing or modifying the variable inside the class methods works as expected.

2. Protected

  • Purpose: Allow access only from the defining class and its subclasses.
  • Example:
class MyClass:
    my_protected = Protected(10)

class Child(MyClass):
    def access_protected(self):
        print(self.my_protected)
  • Behavior:
    • Reading or writing from unrelated classes raises PermissionError.
    • Subclasses can access and modify the value.

3. Internal

  • Purpose: Restrict access to code within the same folder/module.
  • Example:
class MyClass:
    my_internal = Internal(99)
  • Behavior:
    • Access from files outside the same folder raises PermissionError.
    • Useful for module-level encapsulation.

4. Public

  • Purpose: Standard public variable, no access restriction.
  • Example:
class MyClass:
    my_public = Public(123)
  • Behavior:
    • Can be accessed and modified freely from anywhere.

Function Decorators

1. @private

  • Purpose: Restrict method access to the defining class only.
  • Example:
class MyClass:
    @private
    def secret_method(self):
        print("Private Method")
  • Behavior:
    • Calling from outside the class raises PermissionError.

2. @protected

  • Purpose: Allow method access from the defining class and subclasses.
  • Example:
class MyClass:
    @protected
    def prot_method(self):
        print("Protected Method")
  • Behavior:
    • Calling from unrelated classes raises PermissionError.

3. @internal

  • Purpose: Restrict method access to the same folder/module.
  • Example:
class MyClass:
    @internal
    def internal_method(self):
        print("Internal Method")
  • Behavior:
    • Calling from files in different folders raises PermissionError.

4. @public

  • Purpose: Standard public method, no restriction.
  • Example:
class MyClass:
    @public
    def pub_method(self):
        print("Public Method")
  • Behavior:
    • Can be called from anywhere.

5. @AutoPrivateInit

  • Purpose: Required when using @privateinit; ensures instance-level private variables are properly initialized.
  • Example:
@AutoPrivateInit # or @API
class MyClass:
    @privateinit
    def private_init(self):
        self._private_value = Private(10)
  • Behavior:
    • Automatically runs all methods marked with @privateinit after init.
    • Can be called from anywhere, but the decorator itself must be applied to the class whenever @privateinit is used.

5.1. @privateinit

  • Purpose: Initialize private variables separately from __init__(); runs automatically on instance creation.
  • Example:
class Myclass:
    privateVariable = Private(1)
    def __init__(self):
        Myclass.privateVariable = 2
        # Error : will raise PermissionError if a subclass calls super().__init__()
    @privateinit
    def init(self):
        Myclass.privateVariable = 2
        # Right usage
        self.privateVariable2 = Private(2)

Class Decorators

1. @private

  • Purpose: Restrict access to the defining class only; prevents subclass or external code from instantiating or accessing the class directly.
  • Example:
@private
class MyClass:
    @staticmethod
    def private_class():
        print("private class")
  • Behavior:
    • Calling from outside the class raises PermissionError.

2. @protected

  • Purpose: Allow access to the defining class and its subclasses; prevents external code from instantiating or accessing the class directly.
  • Example:
@protected
class MyClass:
    @staticmethod
    def protected_class():
        print("protected class")
  • Behavior:
    • Calling from unrelated classes raises PermissionError.

3. @internal

  • Purpose: Restrict access to the defining class within the same folder/module; prevents external code from instantiating or accessing the class from other folders/modules.
  • Example:
@internal
class MyClass:
    @staticmethod
    def internal_class():
        print("internal class")
  • Behavior:
    • Calling from files in different folders raises PermissionError.

4. @public

  • Purpose: Standard public class; allows unrestricted instantiation and access from any scope.
  • Example:
@public
class MyClass:
    @staticmethod
    def public_class():
        print("public class")
  • Behavior:
    • Can be called from anywhere.

Example Usage

class MyClass:
    my_private = Private(42)
    my_protected = Protected(10)
    my_internal = Internal(99)
    my_public = Public(123)

    @private
    def secret_method(self):
        print(self.my_private)

    @protected
    def prot_method(self):
        print(self.my_protected)

    @internal
    def internal_method(self):
        print(self.my_internal)

    @public
    def pub_method(self):
        print(self.my_public)


class Child(MyClass):
    def access_protected(self):
        print(self.my_protected)
        self.prot_method()

@private
class PrivateClass :
    def __init__(self):
        print("This is a private class!")
        
@internal
class InternalClass :
    def __init__(self):
        print("This is a internal class!")

@protected
class ProtectedClass :
    def __init__(self):
        print("This is a protected class!")

@public
class PublicClass :
    def __init__(self):
        print("This is a public class!")

Notes:

  • Version 0.4.2
  • Python does not natively support strict access control.
  • This library leverages descriptors for variables and decorators for methods.
  • Use with caution, as it relies on call stack inspection (inspect) and may not cover all edge cases.
  • Any class, method, or variable not explicitly marked with an access modifier is considered public by default.
  • Always place class-level access control decorators at the top, above other decorators.

Project details


Download files

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

Source Distribution

pyaccessmodifier-0.4.2.tar.gz (6.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyaccessmodifier-0.4.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

pyaccessmodifier-0.4.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pyaccessmodifier-0.4.2.tar.gz.

File metadata

  • Download URL: pyaccessmodifier-0.4.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for pyaccessmodifier-0.4.2.tar.gz
Algorithm Hash digest
SHA256 9b6f5308d8a7eed36606aa7285319981b24abfb3ae5e7cf1e45e128021521928
MD5 2b71ccabdd39f4882ab787488969cd5b
BLAKE2b-256 ac83587e6a377ffd0cdfefa5821a8c575be8d1eb750ba55bb6da4c35147e380d

See more details on using hashes here.

File details

Details for the file pyaccessmodifier-0.4.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyaccessmodifier-0.4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48460e89de0e7febe59073035f3a9e325eb7bedec60770aa69196436c7a4b7ff
MD5 27d0a23da66addbbd37e5c1e9c0ae971
BLAKE2b-256 a904618201036ea90494a976ed521f532c20c14c3d02ae9c9ae81239ff9e54af

See more details on using hashes here.

File details

Details for the file pyaccessmodifier-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyaccessmodifier-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e91631dff2105f5b6dcdc7b0ef0e301c767fcf33a12dabd4a01fdc83d4369792
MD5 70fb68f328570ca3667a852ae85b3ad8
BLAKE2b-256 f0ef9f89d448de234a2ef9ac1d739f8312fe992ccb9186c9cb44d1bde2e05d96

See more details on using hashes here.

Supported by

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