5 ms·
Asking as a Perl ignoramus, what does this give you over the defaultdict(int) example from the article? Does autovivification mean that the default value to us
by antod 11y ago
Asking as a Perl ignoramus, what does this give you over the defaultdict(int) example from the article?
Does autovivification mean that the default value to use depends on the operation? eg 0 for addition and "" for concatenation?
- buzzdenver 11y agoI'm a Python ignoramus, but I think they're the same, except for Perl's being more concise. How would you do a 3-level hash though in Python ? What you call "value to use depends on the operation" is called casting. Depending on the context, a variable can be different values. For example a variable that has been defined but nothing else is treated as 0 if you try to add a number to it. It's an empty string if you use it in a string operation. Wikipedia explains it well: 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.
- antod 11y ago> I'm a Python ignoramus, but I think they're the same, except for Perl's being more concise. Isn't everything always more concise in Perl? No matter what language it's compared to :) > How would you do a 3-level hash though in Python ? You might/should be able to wrangle that with some customisation of the default_factory or a defaultdict subclass, but yeah the arbitrary depth wouldn't be as 'plug n play' as with Perl. > What you call "value to use depends on the operation" is called casting. Depending on the context, a variable can be different values. For example a variable that has been defined but nothing else is treated as 0 if you try to add a number to it. It's an empty string if you use it in a string operation. Yeah Python's strong typing (usually) balks at automatic casting. You'd need to choose what the default is.
- kbenson 11y ago> Isn't everything always more concise in Perl? No matter what language it's compared to :) Ha, you need more exposure to things like APL :)