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

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

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-1.0.2.tar.gz (3.8 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-1.0.2-py2.py3-none-any.whl (4.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for private_attribute-1.0.2.tar.gz
Algorithm Hash digest
SHA256 d0510fb0e9702b02466505dd26d061c6d5a93a5aeab69ceca716c9897760ae7a
MD5 49f7c01a2d9e6be3a04be5410626498b
BLAKE2b-256 a7290b6229b83d841bcd21ecec7a6958e529321ceb46ce048d3b3bae1dc56efc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for private_attribute-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 230228edc7118a6881f7adee81908eb1710b2d5957f7c9c09b1dbac8a251c1b4
MD5 e1baa270652ce49a3d2c0461e74429cf
BLAKE2b-256 7272af3269b00ea7241aad45ccf72f16f9870108b4bca5da11d5588827aca193

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