Re:Languages to improve your Python
by amyjess on Tuesday October 13, 2015 @10:29AM
(#10380669)
Not just Scheme, but given how important metaprogramming is to Python, I was hoping there would be more Lisp dialects in the list.
[ Parent
| on HN ]
How important is metaprogramming to Python? Frankly, in my 16+ years as Python developer, I have never seen anything that can be remotely described as metaprogramming in Python, except in academic code and experimental GitHub projects.
Re:Languages to improve your Python
by agentultra on Tuesday October 13, 2015 @11:50AM
(#10381246)
You've probably used it without knowing. It's not typically useful in application code but quite useful in libraries.
The ABC module, collections, frozenset. Basically anywhere we add custom types or generate new classes to instantiate.
That surprises me. What does frozenset have to do with metaprogramming? I thought it was just a variant of a hash table or something like that.
Re:Languages to improve your Python
by agentultra on Tuesday October 13, 2015 @02:16PM
(#10382349)
That's an oops on my part. Chalk it up to a baby who likes to wake up at 4:45am and a toddler who wakes up at 7.
frozenset is just a built-in immutable set type with a nice hashing algorithm for the kinds of workloads you'd expect. It's defined in Objects/setobject.c and is a nice read.
I was just rambling and I've used frozenset in metaprogramming code when constructing classes via decorators to implement Lattice types with decorated monotone/morphism methods.
Sorry for the confusion!
That seems like a pretty fair label for what decorators do.
They are a form of metaprogramming, but other than decorators, Python generally discourages other kinds of metaprogramming. Metaclasses are reserved for very specific use-cases, and monkey patching or re-opening class definitions are almost always considered anti-patterns.