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)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file private_attribute-2.3.0.tar.gz.
File metadata
- Download URL: private_attribute-2.3.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a743d85d70a2e3c2157badba1f9d4c4e70198cd1ce4800ddaac09d02cab1e43b
|
|
| MD5 |
ca8b66784849849d390e3c37ed418fc6
|
|
| BLAKE2b-256 |
81fa19b2d689c19c6600b1a426142efe4ea3876970dc1fc8b0b9c12aa3045cfd
|
File details
Details for the file private_attribute-2.3.0-py2.py3-none-any.whl.
File metadata
- Download URL: private_attribute-2.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b836be0b85aba2ae9ff2e92f9c61758fbe0ef1bdfebe2714847ee4014bc0b23d
|
|
| MD5 |
c90e16fa1643384d8e60a57847bbe77e
|
|
| BLAKE2b-256 |
ddf6970d77cebfb59e55e82d6ebe062d74e026939e9b58699b24ccdf15e455a7
|