Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

identicallist-0.1.2.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

identicallist-0.1.2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file identicallist-0.1.2.tar.gz.

File metadata

  • Download URL: identicallist-0.1.2.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.8.2

File hashes

Hashes for identicallist-0.1.2.tar.gz
Algorithm Hash digest
SHA256 07537c653eef69ee4499d1746df5060e78732154b1855c570a3c89dae6174774
MD5 25fc69cb6acc04fdb5d38189687fa5ba
BLAKE2b-256 6b415e995b8643a9f7ae60002f598fcf38844a365b572d862891e1523a16dea6

See more details on using hashes here.

File details

Details for the file identicallist-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: identicallist-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.8.2

File hashes

Hashes for identicallist-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 08130949b1bc74f5f0b49badd897a28a56de5ee33426e76d917dbb5a7dd5c419
MD5 8d0b4f71717e28c3aeb21eaf19e1ae14
BLAKE2b-256 4519ebd014591b7d6a932babff579b7f6d1aa6fcf5397a9737cef4dc2b394349

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page