Skip to main content

Proper access modifiers for Python classes.

Project description

pyattr

PyPI PyPI - Downloads Lines of Code

While Python does have name mangling, it is not nearly as powerful as access modifiers found in languages such as C++. pyattr provides an easy-to-use API for access modifiers in Python.

Installation

Installation via pip:

pip install pyattr

Usage

All you have to do is make your class inherit from the pyattr.Pyattr class, and add super().__init__() as the first line in the __init__ function of your class. And that's it! pyattr will handle the magic to make sure variables cannot be accessed / set where the shouldn't be. It also provides useful error messages to users.

Example

Here is a simple examples involving a private variable.

from pyattr import Pyattr

class Example(Pyattr):
    def __init__(self) -> None:
        super().__init__()

        self.__name = "pyattr"

example = Example()
print(example.__name) # Error - '__name' is a private attribute of 'Example'.

As well as variables, pyattr also supports access control of functions!

from pyattr import Pyattr

class Example(Pyattr):
    def __init__(self) -> None:
        super().__init__()

    def __example(self) -> None:
        pass


example = Example()
print(example.__example())  # Error - '__example' is a private attribute of 'Example'.

How does it work?

Note For a more in-depth explanation on how pyattr works, see my blog post.

pyattr overrides the default set and get functions of your class. The overridden functions defined by pyattr are merged into your class when you inherit from the pyattr.Pyattr class. As well as this, the pyattr.Pyattr class inherits from the pyattr._PyattrDict class, which provides a custom dictionary implementation. This is because you can change the variables in a class using class.__dict__["var"] = "val", meaning a custom dictionary would be the best way to prevent the access system being circumvented.

The overriden set and get functions of your class call the respective set and get functions of the custom dictionary. This dictionary, using inspect.stack(), works out the caller's function, and the caller's class (if any). It uses this data to work out if the caller should be allowed to access the specified variables. If it shouldn't, an AttributeError is raised, with an error message explaining the cause.

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

pyattr-1.3.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

pyattr-1.3.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file pyattr-1.3.2.tar.gz.

File metadata

  • Download URL: pyattr-1.3.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyattr-1.3.2.tar.gz
Algorithm Hash digest
SHA256 8b5540d3f6b3699871f5e6fd006c8a389a68ffadfc6dc5d727740f36fb1f3e97
MD5 bc4743668c029f414c819f05cc2b0a26
BLAKE2b-256 741dd3fdb8762aa3c2b94d006bbcfeef71c414516e90fbb993f3847209a043b8

See more details on using hashes here.

File details

Details for the file pyattr-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: pyattr-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyattr-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1e47b8e1839b3e367c3b7ce6006e497c1c8febf6a6b5ca0bb2daf7752104e88
MD5 d49b453bbc11b06c71709c891654577f
BLAKE2b-256 a91f0d1f6514869095d07f514062f953d5aea6739e37e5ff1bc826e8a8e77f74

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