Friday, March 17, 2017

Autovivification in Python ∞

Autovivification in Python

In the Perl programming language, autovivification is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced. Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand (for more info click here). 

In this post, we implement Autovivification in Python as in the following.


import collections

class PythonAV(dict):
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value


example = PythonAV()
example[1][2][3] = 8
example[1][3][3] = 9
example[1][2]['string_val'] = 10

print(example)





Copyright ©2017, GRREF, All rights reserved.
See Contents

1 comment:

  1. Grref: Autovivification In Python ∞ >>>>> Download Now

    >>>>> Download Full

    Grref: Autovivification In Python ∞ >>>>> Download LINK

    >>>>> Download Now

    Grref: Autovivification In Python ∞ >>>>> Download Full

    >>>>> Download LINK be

    ReplyDelete