Shy tools.
Project description
shytools
The shytools module provides tools to implement shy classes, that is, classes whose instances have real private attributes:
-
shymethod- Shy methods decorator. -
shyproperty- Shy properties decorator. -
shytype- Shy abstract base class.
Usage
`from shytools import *`
`class MyShy(shytype):`
`__slots__ = ()`
`@shymethod`
`def __init__(self):`
`self.private = "spam"`
`@shymethod`
`def get(self):`
`return self.private`
`@shymethod`
`def set(self, value):`
`self.private = value`
`@shyproperty`
`def public(self):`
`return self.private`
`@public.setter`
`def public(self, value):`
`self.private = value`
`my_shy = MyShy()`
`print(my_shy.get()) # "spam"`
`print(my_shy.public) # "spam"`
`my_shy.set("eggs") # ok`
`print(my_shy.get()) # "eggs"`
`print(my_shy.public) # "eggs"`
`my_shy.public = "spam" # ok`
`print(my_shy.get()) # "spam"`
`print(my_shy.public) # "spam"`
`print(my_shy.private) # AttributeError`
Issues and limitations
-
The shy subclasses must necessarily define an
__slots__attribute, and that attribute must necessarily be an empty tuple:`class MyShy(shytype):` `__slots__ = ()` -
Within shy properties and methods, self is not a real instance of the shy subclass:
`class MyShy(shytype):` `__slots__ = ()` `@shymethod` `def __init__(self):` `print(isinstance(self, MyShy)) # False` `def regular_method(self):` `print(isinstance(self, MyShy)) # True` -
If the shy subclasses implement their own
__new__and__del__, they need to call__new__and__del__of the base class manually:`class MyShy(shytype):` `__slots__ = ()` `def __new__(cls, *pargs, **kwargs):` `my_shy = shytype.__new__(cls)` `<do something>` `return my_shy` `def __del__(self):` `<do something>` `shytype.__del__(self)`
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
File details
Details for the file shytools-1.0.1.tar.gz.
File metadata
- Download URL: shytools-1.0.1.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c65ab71c0b3cf8d11f6a232c2a3b4e1e4b1898fcb55a594f0884dad96cac494
|
|
| MD5 |
73b3bf89743caa41a4d52a11d8559a55
|
|
| BLAKE2b-256 |
ef1ad051fced136074f1e637e4b0a9bea15e9ee5eb899f0016c40fa7e1be189e
|