Re:Show HN: Rust-Like Traits in Python
by xrudelis on Sunday November 21, 2021 @05:29PM
(#29300412)
I think these are all equally good. The Impl's name should never need to be referred to, so ultimately the choice should be based on what's easiest to read.
EDIT: actually, your examples wouldn't work as-is, since it would result in circular references (something Python isn't well-equipped to handle). Your target names would need to be strings instead of the actual class.
EDIT2: I have updated the code to allow for another syntax:
class AnyImplNameIWant(MyTrait, metaclass=Impl, target="MyStruct"):
...
It would certainly be nicer if `MyStruct` didn't need to be a string of course. I really think these options are mostly down to taste, so I'll just have to trust that this alternative reads sufficiently more nicely.
Re:Show HN: Rust-Like Traits in Python
by aldanor on Sunday November 21, 2021 @05:52PM
(#29300581)
Correction: what's easiest to read to someone who hasn't carefully read every single line of your library. Verbosity is not always bad, if it yields clarity.
EDIT: actually, your examples wouldn't work as-is, since it would result in circular references (something Python isn't well-equipped to handle). Your target names would need to be strings instead of the actual class.
EDIT2: I have updated the code to allow for another syntax:
It would certainly be nicer if `MyStruct` didn't need to be a string of course. I really think these options are mostly down to taste, so I'll just have to trust that this alternative reads sufficiently more nicely.