News for hackers. Stuff that matters.
Re:Frankenstein's `__init__` by fouronnes3 on Saturday April 19, 2025 @11:10AM (#43736942)
How would you do traits in Python?
Re:Frankenstein's `__init__` by svilen_dobrev on Saturday April 19, 2025 @12:12PM (#43737350)
there were "traits" in python :/ . Search for pyprotocols.

(searching myself.. not much left of it)

2004 - https://simonwillison.net/2004/Mar/23/pyprotocols/

some related rejected PEP .. https://peps.python.org/pep-0246/ talking about something new to be expected.. in 2001?2005?

no idea did it happen and which one would that be..

Re:Frankenstein's `__init__` by dragonwriter on Saturday April 19, 2025 @02:49PM (#43738530)
Possibly Guido, in that rejection, was talking about what ended up as ABCs, original PEP: https://peps.python.org/pep-3119/

Further work was done in this area, building on ABCs, with Protocols, original PEP: https://peps.python.org/pep-0544/

Re:Frankenstein's `__init__` by echelon on Saturday April 19, 2025 @11:20AM (#43737004)
I'd really need to think long and hard about it, but my initial feeling is that we'd attach them to data classes or a similar new construct. I don't think you'd want to reason about the blast radius with ordinary classes. Granted, that's more language complexity, creates two unequal systems, and makes much more to reason about. There's a lot to juggle.

As much fun as putting a PEP together might be, I don't think I have the bandwidth to do so. I would really like to see traits in Python, though.

Re:Frankenstein's `__init__` by vaylian on Saturday April 19, 2025 @01:19PM (#43737826)
Python has a trait-like system. It's called "abstract base classes": https://docs.python.org/3/library/abc.html

The abstract base class should use the @abstractmethod decorator for methods that the implementing class needs to implement itself.

Obviously, you can also use abstract base classes in other ways, but they can be used as a way to define traits/interfaces for classes.

Re:Frankenstein's `__init__` by dragonwriter on Saturday April 19, 2025 @02:45PM (#43738492)
Protocols, too (they are defined on top of ABCs)

https://typing.python.org/en/latest/spec/protocol.html#proto...

Re:Frankenstein's `__init__` by gpderetta on Saturday April 19, 2025 @05:48PM (#43739726)
Protocols are definitely the closest thing to traits in Python. ABC are pretty much OoO interfaces.