Skip to main content

Implementation of Value Object Pattern for Python

From Wikipedia:

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.

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)

Source distribution for value 0.1.0
File Size Uploaded
value-0.1.0.tar.gz 3.4 kB 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page