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.


Variable Descriptors

1. Private

  • Purpose: Restrict access to the variable only within the defining class.
  • Example:
class MyClass:
    my_private = Private(42)
  • Behavior:
    • Reading or writing from outside the class raises PermissionError.
    • Only instances of the defining class can access the value.

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.

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()

Notes:

  • 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.

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.3.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

pyaccessmodifier-0.3.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyaccessmodifier-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b692538aac2cea0b8a140982d1ccbb73fac7c703df7a13902bb5072618144b5b
MD5 3b03bfab281b9f816ab6578d3dbfb135
BLAKE2b-256 c56737874e989e5faf0aa07425a87c54e03751af8e23c8363fbb2180b7012ae7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyaccessmodifier-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ea4794a2489eb5ea1ce01332c5e3962a2d3a1d50edd542dc89c67b393d9a3cc
MD5 3536fcf861678e8f9f45f76ef013b269
BLAKE2b-256 05a2526bc308e6c72e3e35da80d4c67e6dc37e628b716c55feb8a6120ed380cc

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