by imaurer on 1/5/22, 4:35 PM with 124 comments
by pengwing on 1/5/22, 5:53 PM
by zzzeek on 1/5/22, 5:40 PM
by echelon on 1/5/22, 5:26 PM
This is so hard to reason about and fix at scale. If you let other engineers run wild with this and build features with it, the time will eventually come to decom your service and move functionality elsewhere. Having to chase down these rabbits, duplicate magic, and search large code bases without the help of an AST assisted search is slow, painful, and error prone.
I spent several years undoing magic method dispatch in Ruby codebases. Tracing through nearly a thousand endpoints to detect blast radiuses of making schema changes impacted by CRUD ops on lazily dispatched "clever code".
I'm sure there are valid use cases for this, but be exceedingly careful. Boring code is often all you need. It doesn't leave a mess for your maintainers you'll never meet.
Python users tend not to behave this way, but seeing posts like this requires me to urge caution.
by chadykamar on 1/5/22, 5:59 PM
— Tim Peters
by lvass on 1/5/22, 5:27 PM
by imaurer on 1/5/22, 4:38 PM
https://twitter.com/dabeaz/status/1466731368956809219
Which is a warning that people shouldn't buy his new book if they want a deep dive on Metaprogramming:
https://www.amazon.com/dp/0134173279/
Simon does a nice job demonstrating how "the __init_subclass__ class method is called when the class itself is being constructed."
by VWWHFSfQ on 1/5/22, 7:30 PM
graph = {
key: {
p
for p in inspect.signature(method).parameters.keys()
if p != "self" and not p.startswith("_")
}
for key, method in cls._registry.items()
}
The first parameter to a bound method does not have to be called `self`. It's conventional, but not required. Is there a better way in the inspect module to filter these parameters? This comes up more often with classmethods where the naming convention `cls` is most common but I see `klass` somewhat frequently as well.by matsemann on 1/5/22, 5:17 PM
by zbyforgotpass on 1/5/22, 5:19 PM
by 3pt14159 on 1/5/22, 5:08 PM
by Naac on 1/5/22, 7:00 PM
by captainmuon on 1/5/22, 6:16 PM
class MainWindow(gtk.Window):
__metaclass__ = MetaDescribedUI("MainWindow.ui")
def __init__(self):
# now you can use e.g. self.lvFiles or self.btnOK
pass
Although writing it now I could probably do it differently without metaclasses.by kkirsche on 1/5/22, 5:50 PM
by fdgsdfogijq on 1/5/22, 6:09 PM
"The Jimi Hendrix of programming"
by joebergeron on 1/5/22, 7:01 PM
For those curious about what metaclasses actually are, I wrote up an article with some motivating examples about this a little while back. Might be worth a read if you’re interested!
https://www.joe-bergeron.com/posts/Interfaces%20and%20Metacl...
by wheelerof4te on 1/5/22, 10:44 PM
People unironically created interfaces and various overloaded operations using dunder methods. Not only do you need to keep a list of them under your pillow, but you must also keep notes on how to construct the interface for each one.
And they look ugly as hell. In a language that prides itself on readability and user friendlines.
My suggestion is to ditch the OOP syntax completely and make a built-in analogue to a C struct. Take inspiration from dataclasses syntax. OOP in a dynamic glue language is a silly idea anyway. Of course, you would have to bump the version to 4.0.
by tsujamin on 1/5/22, 10:12 PM
class MyCustomOptionsFlow(OptionsFlow, DOMAIN="MYPLUGIN"):...
A bit of digging showed that __init__subclass_ was being used effectively as a registration callback, adding the newly defined class/DOMAIN to the global CustomOptions handler at class definition.Very neat, not immediately obvious however :/
by cutler on 1/5/22, 9:27 PM
by sam0x17 on 1/5/22, 6:19 PM
by jwilk on 1/6/22, 11:00 AM
https://docs.python.org/3/reference/datamodel.html#object.__...
by wheelerof4te on 1/5/22, 8:02 PM
Let's say you have this code:
class Creature:
def __init__(self, name: str, hp: int, attack: int):
...
def attack(self, other: Creature):
...
Guess what? Since Creature is not yet defined, your very smart type-checker can't see the "other" parameter. Happened to me in VS Code.That is the problem with features bolted on to a language where they perhaps don't belong. Now, I know that typing helps when you have simple, functional code. So, this code will work:
from dataclasses import dataclass
@dataclass
class Creature:
name: str
hp: int
attack: int
def combat_between(attacker: Creature, defender: Creature):
...I was really surprised with this. We need better review in regards to adding additional features that may be broken on certain setups.
by ajkjk on 1/5/22, 7:54 PM
The one that bites me a lot is: I want to easily be able to write a decorator that accesses variables on a particular _instance_ of a class (such as a cached return value), and I want to take a Lock when I do it.
But since decorators are defined on the class method, not the instance, it has to do a bunch of work when the function runs: look at the instance, figure out if the lock is defined, if not define it (using a global lock?), take the lock, then do whatever internal processing it wants. It feels like decorators should have a step that runs at `__init__` time to do per-instance setup but instead I have to figure it out myself.
by wheelerof4te on 1/5/22, 6:01 PM
by Garlef on 1/6/22, 7:40 AM
@decorator
class SomeClass:
...
To me, class decorators seem to be much easier to reason about.by amelius on 1/5/22, 8:47 PM
by dreyfan on 1/5/22, 5:50 PM