Skip to main content

Private attribute decorator for Python classes

Project description

Private Attribute

Introduction

This package provide a way to create the private attribute like "C++" does.

Usage

This is a simple usage about the module:

from private_attribute import PrivateAttrBase

class MyClass(PrivateAttrBase):
    __private_attrs__ = ['a', 'b', 'c']
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3

    def public_way(self):
        print(self.a, self.b, self.c)

obj = MyClass()
obj.public_way()  # (1, 2, 3)

print(hasattr(obj, 'a'))  # False
print(hasattr(obj, 'b'))  # False
print(hasattr(obj, 'c'))  # False

All of the attributes in __private_attrs__ will be hidden from the outside world, and stored by another name.

You can use your function to generate the name. It needs the id of the obj and the name of the attribute:

def my_generate_func(obj_id, attr_name):
    return some_string

class MyClass(PrivateAttrBase, private_func=my_generate_func):
    __private_attrs__ = ['a', 'b', 'c']
    def __init__(self):
        self.a = 1
        self.b = 2
        self.c = 3

    def public_way(self):
        print(self.a, self.b, self.c)

obj = MyClass()
obj.public_way()  # (1, 2, 3)

If the method will be decorated, the property, classmethod and staticmethod will be supported. For the other, you can use the PrivateWrapProxy to wrap the function:

from private_attribute import PrivateAttrBase, PrivateWrapProxy

class MyClass(PrivateAttrBase):
    __private_attrs__ = ['a', 'b', 'c']
    @PrivateWrapProxy(decorator1())
    @PrivateWrapProxy(decorator2())
    def method1(self):
        ...

    @method1.attr_name
    def method1(self):
        ...

    @PrivateWrapProxy(decorator3())
    def method2(self):
        ...

    @method2.attr_name
    def method2(self):
        ...

Notes

  • All of the private attributes class must contain the __private_attrs__ attribute.
  • The __private_attrs__ attribute must be a sequence of strings.
  • You cannot define the name which in __slots__ to __private_attrs__.
  • When you define __slots__ and __private_attrs__ in one class, the attributes in __private_attrs__ can also be defined in the methods, even though they are not in __slots__.
  • All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.
  • Finally the attributes' names in __private_attrs__ will be change to a tuple with two hash.

License

MIT

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

private_attribute-2.4.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

private_attribute-2.4.0-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file private_attribute-2.4.0.tar.gz.

File metadata

  • Download URL: private_attribute-2.4.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for private_attribute-2.4.0.tar.gz
Algorithm Hash digest
SHA256 3697edbb6decb1200ea8e9cd4551565f7762b7da3995d5693724571baf03157d
MD5 71008ed98b372ba09bdc2d3f35ac8741
BLAKE2b-256 c42d10a4359af425059828f9a2a7ed7089c55d417c5c6fa68cb9422635c82b93

See more details on using hashes here.

File details

Details for the file private_attribute-2.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for private_attribute-2.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7521207acb949b34a254438cb1f3ddbb0bb0d63dfe527507a49c38da200dad87
MD5 c20ee0521ea36a2287fd249d10273937
BLAKE2b-256 02c440a56e9d44f218d6b376058fcd46e023e0396e1a61f294872729bb264d16

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