more_properties
A collection of property variants.
Basic Usage
Variants behave mostly as the built-in property, except where noted.
Given the following class,
from more_properties import property, class_property, static_property
class Parrot:
@property
def name(self):
return "Fred"
@class_property
def order(cls):
return Psittaciformes
@static_property
def planet():
return Earth
the properties may be accessed like so:
>>> Parrot().name
'Fred'
>>> Parrot.order
<class 'Psittaciformes'>
>>> Parrot.planet
<class 'Earth'>
Setters/Deleters
Setters and deleters are defined in the same way as the built-in property.
Either with the decorator method
from more_properties import class_property
class Foo:
name = "Foo"
@class_property
def identifier(cls):
"""Object identifier"""
return cls.name.lower()
@identifier.setter
def identifier(cls, value):
cls.name = value.title()
@identifier.deleter
def identifier(cls):
cls.name = None
or the inline method
from more_properties import class_property
class Foo:
name = "Foo"
@classmethod
def get_identifier(cls):
return cls.name.lower()
@classmethod
def set_identifier(cls, value):
cls.name = value.title()
@classmethod
def del_identifier(cls):
cls.name = None
identifier = class_property(
get_identifier,
set_identifier,
del_identifier,
"Object identifier"
)
Reference
property
A modified version of the built-in property.
Always behaves as a data descriptor, regardless of which (if any) of getter, setter, and deleter are set.
Behaviour when accessed on a class, is undefined.
class_property
A property for classes.
Both cls.x and instance.x call the getter with the class.
Setting instance.x calls the setter with the class and value.
Deleting instance.x call the deleter with the class only.
from more_properties import class_property
class Foo:
@class_property
def identifier(cls):
"""Class identifier"""
return cls.__name__.lower()
class Bar(Foo):
pass
>>> Foo.identifier
'foo'
>>> Foo().identifier
'foo'
>>> Bar.identifier
'bar'
>>> Bar().identifier
'bar'
classproperty provided as a synonym, for consistency with classmethod.
static_property
A property independent of its accessor.
Both cls.x and instance.x call the getter with no parameters.
Setting instance.x calls the setter with the value only.
Deleting instance.x call the deleter with no parameters.
from more_properties import static_property
x = "bar"
class Foo:
@static_property
def val():
return x
>>> Foo.val
'bar'
>>> Foo().val
'bar'
staticproperty provided as a synonym, for consistency with staticmethod.
cached_property
cached_class_property
cached_static_property
Variants of property, class_property, and static_property, respectively.
They are each used in the same way as the originals, but cache the value of the getters.
from dataclasses import dataclass
from more_properties import cached_property
@dataclass
class Foo:
x: int
@cached_property
def y(self):
print("Doing work")
return self.x + 1
>>> bar = Foo(1)
>>> bar.y
Doing work
2
>>> bar.y
2
If the setters/deleters are defined, then the cache is cleared before they are called.
Further, the cache may be explicitly cleared through the clear_cache method,
exposed only during class creation.
@dataclass
class Foo:
x: int
@cached_property
def y(self):
print("Doing work")
return self.x + 1
y_clear_cache = y.clear_cache
>>> bar = Foo(1)
>>> bar.y
Doing work
2
>>> bar.y
2
>>> bar.y_clear_cache()
>>> bar.y
Doing work
2
Installation
Install and update using the standard Python package manager pip:
pip install more_properties
Release files for more-properties 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| more_properties-1.1.1.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| more_properties-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.2 kB
Release files / more_properties-1.1.1.tar.gz
| Download URL | more_properties-1.1.1.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
47b136857d89c72b53def0fade2f2e0a23a95071c3eb87d5a77cfc91554f106c
|
|
BLAKE2b-256 checksum How to use checksums |
9279bf355e368c07fcf55a6a69e79af00addfa15e0e8a58eea14a39281e5721e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.7
|
Release files / more_properties-1.1.1-py3-none-any.whl
| Download URL | more_properties-1.1.1-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0992c49041cb2600e5ff4d8414bdf5761551f5404d00a4e553ced4f14d34b0bb
|
|
BLAKE2b-256 checksum How to use checksums |
16b018f46d8917d2d7806df5c4f5c1f529fc7d3cf305106dddf304fe1fe48aa3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.7
|