4 ms·
If anyone's wondering how you get autoviv in Python, its surprisingly gross/elegant/fun depending on how much you use the phrasing "pythonic" in your interactio
by Ultimatt 8y ago
If anyone's wondering how you get autoviv in Python, its surprisingly gross/elegant/fun depending on how much you use the phrasing "pythonic" in your interaction with people.
some_autoviv_variable = defaultdict(lambda: defaultdict(dict))
- braythwayt 8y agoCool! Does that create defaultdicts “all the way down?”
- Ultimatt 8y agoAt least until it hits the turtle bedrock at the end of your system memory. Oh maybe you need the inner one to be a lambda of defaultdict actually, then it will. turtles = lambda: defaultdict(turtles) some_autoviv_variable = defaultdict(turtles) Have to confess I'm a polyglot type person, and even Im starting to twitch at "unpythonic" feels of having something as nuts as a recursive expression in a lambda with some assignment.
- klibertp 8y agoI could agree with "unpythonic", as Python is a bit weak in this department, but "recursive lambda" being "nuts" is a bit much - especially here, given the site's name ;)
- Ultimatt 8y agoOh yeah only nuts given we are in Python land. It's kind of like if some super advanced alien artefact landed on Earth. It would appear as magic incongruent with reality. Thats Python and almost anything thats expressive. God forbid your brain can imagine something beyond simple statements.
- braythwayt 8y ago> Im starting to twitch at "unpythonic" feels of having something as nuts as a recursive expression in a lambda with some assignment. Welcome to the Dark Side. Enjoy the cookies!
- joshuamorton 8y agoNuts it may be, but rename it trie = defaultdict(lambda: defaultdict(trie)) and you have a 1-line trie data structure, which is super useful.
- Ultimatt 8y agoNot sure how it's a trie at all. Nothing is imposing prefixes here. But it is a tree sure.