A mutable list that allows only identical objects and inherits their properties and methods.
Project description
identicallist
A mutable Python list that allows only identical objects and inherits their properties and methods.
If a list property changes, it changes this property in all items. If a list method is invoked, it invokes the method on all items.
Examples
IdenticalList behaves just like a list:
from identicallist import IdenticalList
class DummyItem:
def __init__(self):
self.num = 5
def inc(self):
self.num += 1
def dec(self):
self.num -= 1
il = IdenticalList(DummyItem(), DummyItem())
print(len(il)) # 2
print(iter(il)) # <generator object Sequence.__iter__ at 0x7fc3b708d5d0>
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 5
# item number 1 has the attribute num with value 5
At the same time, it also behaves like the first object of the list:
print(il.num) # 5
il.inc()
print(il.num) # 6
Changing properties and calling methods on the list object affects all items in the list:
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 6
# item number 1 has the attribute num with value 6
il.dec()
il.dec()
print(il.num) # 4
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 4
# item number 1 has the attribute num with value 4
il.num = 7
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 7
# item number 1 has the attribute num with value 7
Changing properties on individual items in the list only affect that item:
il[1].num = 8
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 7
# item number 1 has the attribute num with value 8
il[0].dec()
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 6
# item number 1 has the attribute num with value 8
Now, calling inc on the list increases all item's num property. Note that because they started with different values, the resulting values are still different:
il.inc()
for i, item in enumerate(il):
print('item number {} has the attribute num with value {}'.format(i, item.num))
# item number 0 has the attribute num with value 7
# item number 1 has the attribute num with value 9
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
Hashes for identicallist-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08130949b1bc74f5f0b49badd897a28a56de5ee33426e76d917dbb5a7dd5c419 |
|
MD5 | 8d0b4f71717e28c3aeb21eaf19e1ae14 |
|
BLAKE2b-256 | 4519ebd014591b7d6a932babff579b7f6d1aa6fcf5397a9737cef4dc2b394349 |