Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Ahh so if a method on an object dynamically adds in the quack() method at runtime, does the object spontaneously change type?

Yes, actually.

This is an antipattern in the vast majority of use cases. There are far better patterns out there than just sticking a method on an object, and I detest codebases that do this sort of thing. It's almost always someone who uses the phrase "pythonic" unironically way too much, writing these sorts of things, and a nightmare to debug.

There's less problem with doing something like

    def quackerizer(foo: Any) -> Quacker:
        foo.quack = lambda...
        return foo

Still kinda odd, but at least you are doing a formal cast.

> That's an IDE problem, with sufficently powerful code inspection, the fields can still be found.

Ok, name one. Pycharm and vscode don't currently do it afaik. Maybe some vscode plugin, haven't used vscode much.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: