News for hackers. Stuff that matters.
Re:Python: Uncovering the Overlooked Core Functionalities by learplant on Monday July 24, 2023 @08:51PM (#36856340)
I find that __call__ is very confusing, but maybe because I'm not used to seeing if often.

What is the benefit compared to having a method named "add" that also explains the behavior?

Re:Python: Uncovering the Overlooked Core Functionalities by Jtsummers on Monday July 24, 2023 @08:58PM (#36856408)
If an object is callable you can use it in places that might conventionally expect functions. The utility of that is very situational, though. I've only used it a handful of times myself over the years I've known and used Python.

It may also give you a "clearer" (in quotes because subjective) presentation for something you're trying to do.

Re:Python: Uncovering the Overlooked Core Functionalities by claytonjy on Monday July 24, 2023 @10:21PM (#36857078)
I see it a lot in HuggingFace, and use it myself for classes that are used like a function, especially when the obvious method name is the verb form of the class name

    processor = SomeProcessor.load("path/to/config")

    # with __call__
    processed_inputs = processor(inputs)

    # less awkward than
    processes_inputs = processor.process(inputs)
The only benefit is to the human, same as @property or even @dataclass.
Re:Python: Uncovering the Overlooked Core Functionalities by learplant on Tuesday July 25, 2023 @05:51PM (#36870089)
Thanks for writing that up! I disagree though, I prefer the processor.process for clarity, and for not adding another way of doing things that regular methods already do.