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__.
  • All of the object that is the instance of the class "PrivateAttrBase" or its subclass are default to be unable to be pickled.

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.0.1.tar.gz (5.6 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.0.1-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: private_attribute-2.0.1.tar.gz
  • Upload date:
  • Size: 5.6 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.0.1.tar.gz
Algorithm Hash digest
SHA256 9bfd13217ebb49b6b03a71125680b77d9d575c06fe924699c3139ea598491845
MD5 e09f7009c18ba5cfceb3fef30b256e7e
BLAKE2b-256 32223043b95bcab97f7f4953ef347aacd7d3f58252d5418f265879f08062e892

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for private_attribute-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7751014047b6a803091915dfa178932e125aa87c62e60908870891839f98007f
MD5 52782648f35680a5bed62bd07871729c
BLAKE2b-256 34f58491a3c6b0db7c48d2f6fb81cffe47b9813251f00aae3ff3f3afac2e300c

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