This is awesome! Now, never ever do it. Python is powerful and flexible enough that you don't need metaprogramming. (I mean this quite literally: I sincerely doubt that there any code in Python using metaclasses etc., that wouldn't be more clear and maintainable if rewritten in "plain old" Python without them.) (With the caveat that I'm not including "art" projects, I'm talking about working production code.) (In cas…
Metaprogramming in Python
51–60 of 61 posts
Re: Metaprogramming in Python
#52It’s interesting that meta programming, one of the main selling points of lisps, is actually pretty common and possible in other languages as well. Lisp’s “code = data” achieves meta programming in a straightforward way, but this link shows that it’s not necessary. In fact, Python’s way might even be better because the language sorta “gets outta the way” because it’s really darn simple.
Python recently got a form of structural pattern matching.
Can that be removed from Python, and then reimplemented only in Python? I mean other than by writing a .py file to .py file text filter?
Can it be backported to a prior version of Python?
Python's release history is full of "can't use this syntax if you're not on at least x.y.z version"; it continuously proves that it has it doesn't have metaprogramming on a level that could be used to develop the language. When the purveyors of Python decide to add some new syntax and semantics, they eschew Python metaprogramming and dive straight into C.
Re: Metaprogramming in Python
#53It is shocking how much vodoo magic there is in a programming language marketed as "easy to learn and use". And with the recent addition of new syntax, I would hardly call Python easy.
I’ve been writing Python professionally for over 20 years, and I’ve needed to use the things here once. It was highly encapsulated inside one module of an ORM-type project, where other code wanted to use it without having to know anything about its implementation details. You can do these things in the extremely rare cases that you must , but other than that you shouldn’t .
Re: Metaprogramming in Python
#54Earlier quoted context omitted.
I’ve been writing Python professionally for over 20 years, and I’ve needed to use the things here once. It was highly encapsulated inside one module of an ORM-type project, where other code wanted to use it without having to know anything about its implementation details. You can do these things in the extremely rare cases that you must , but other than that you shouldn’t .
I’m surprised to hear your opinion on this. I have around 12 years of python experience and I find metaprogramming to be, by far, the most important part of Python. Without the extensive ability to rewrite underlying functionality in a way that was approachable to both novice and adept users, I don’t think we’d have seen a widespread adoption of the language to begin with.
Re: Metaprogramming in Python
#55Earlier quoted context omitted.
I’m surprised to hear your opinion on this. I have around 12 years of python experience and I find metaprogramming to be, by far, the most important part of Python. Without the extensive ability to rewrite underlying functionality in a way that was approachable to both novice and adept users, I don’t think we’d have seen a widespread adoption of the language to begin with.
I was referring particularly to metaclasses. Decorators, etc.? All the time.
Re: Metaprogramming in Python
#56Earlier quoted context omitted.
I personally consider code generation an anti-pattern in Python. With its dynamic nature and the two-step execution model, Python is essentially its own macro language, and any generation can be done at runtime.
unfortunately pep-484 changed all that. now you have to use static generation for basically anything that used to be at runtime previously, if you want it to have any kind of compatibility with mypy, pylance, etc.
Re: Metaprogramming in Python
#57Earlier quoted context omitted.
I was referring particularly to metaclasses. Decorators, etc.? All the time.
I guess I don’t often touch metaclass either, although after reading this article it gave me some ideas on how I might better implement object validation patterns. That being said, I also have recently become acquainted with pydantic, which does take care of some of that.
Re: Metaprogramming in Python
#58It is shocking how much vodoo magic there is in a programming language marketed as "easy to learn and use". And with the recent addition of new syntax, I would hardly call Python easy.
Re: Metaprogramming in Python
#59Earlier quoted context omitted.
unfortunately pep-484 changed all that. now you have to use static generation for basically anything that used to be at runtime previously, if you want it to have any kind of compatibility with mypy, pylance, etc.
MyPy doesn't require everything to be type-hinted. It's fine to sprinkle in hints where you want them.
Re: Metaprogramming in Python
#60The article's reference to "classobj" is wrong for Python 3 (that was the type of old-style classes in Python 2). In Python 3: >>> class SomeClass: ... pass ... >>> type(SomeClass)