Implementation of Value Object Pattern for Python
A value object is a small object that represents a simple entity whose equality isn’t based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.
By default (if you subclass from object) Python follows “reference semantics”, i.e. two objects are equal if they are the same instance. Value class implements “value semantics”, i.e. if you subclass it your objects will be equall if they hold the same data.
This implementation will also inspect your __init__ signature to automatically assign instance variables and produce a nice __repr__ for your objects, dogether with a suitable __hash__ implementation.
Instead of asigning each instance variable manually:
>>> class Date(object):
...
... def __init__(self, year, month=1, day=1):
... self.year = year
... self.month = month
... self.day = day
Value defines __new__ that will look at your __init__ signature and assign instance variables based on it:
>>> from value import Value
...
>>> class Date(Value):
...
... def __init__(self, year, month=1, day=1):
... pass
...
>>> Date(2013, 3).year == 2013
True
>>> Date(2013, 3).month == 3
True
>>> Date(2013, 3).day == 1
True
Value defines __eq__ and __ne__ to implement value object semantics, i.e. objects holding the same data are compared equal:
>>> Date(2013, 3, 18) == Date(2013, 3, 18)
True
>>> Date(2013, 3, 18) != Date(1988)
True
Value also defines __repr__ for you based on __init__ signature:
>>> repr(Date(2013, 3, 18))
'Date(2013, 3, 18)'
>>> repr(Date(1988, 1, 1))
'Date(1988)'
Value also defines __hash__ for you, so that instances could be used in sets and as dictionary keys.
Installation
Use pip or easy_install:
pip install value==0.1.0
Alternatively, you can just drop value.py file into your project—it is self-contained.
value is tested with Python 2.6, 2.7, 3.2, 3.3 and PyPy.
value follows semantic versioning.
Metadata
Release files for value 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| value-0.1.0.tar.gz | 3.4 kB | Without provenance | Details |
Release files / value-0.1.0.tar.gz
| Download URL | value-0.1.0.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d6f696f9ac0d5ecb6189f6fe08e153cc6ee39449c3b23100e565b156a60c569
|
|
BLAKE2b-256 checksum How to use checksums |
13d533f2063de7e21cf41cd6c029d2212cea9da7062e45cad37b3942bf935f7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for value 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.