On 28/04/20 7:36 PM, Chris Angelico wrote:
"Best"? Not sure about that. Functions are first-class objects in
Python, so a function *is* a callable object. You don't have to create
a custom class with a call method just to be able to attach attributes
to your function.

ChrisA


Using a mutable object as a function default parameter value
and changing it inside the function looks like a "trick"
according to me.

Sure. But you're contrasting this to a suggestion to literally just
attach attributes to a function. Python lets you actually do that. You
don't have to simulate the feature by creating a custom class and
making it callable - you just straight-up add attributes to a
function. Sure, what you suggested works, but there's no reason to.


Functions are objects too! I regularly point-out this powerful facility, and its affordances, but...


Yes, it's perfectly reasonable and even sensible to attach an attribute; BUT do many people expect to find such? If we were to collectively survey our own application code, how many examples would we find - as a percentage of such a corpus?

Expectation: it would be v.v.low. Accordingly, whilst perfectly-implemented Python, and thus not a "trick", at least it is something that is easy for 'an ordinary person' to 'miss' (or misunderstand).

The same cognitive logic applies to function parameters. Use of these is laced with 'gotchas', because people assume a different logic to/fail to properly understand Python. Hence such style decisions as 'use None, or not at all'.

Personal opinion: I've never really liked closures, and have tended to associate them with other languages that actually need them in order to accomplish common-constructs or have them as a tenet/pillar of their language-design philosophy. Python does not, so...

The choice of global variable should not be completely discounted - we have been given the global statement for a reason! However, its use in this case has been rightly-criticised (elsewhere) as an unnecessary 'pollution'.

Which leaves us (or me!) with the overly-wordy wrapping-in-a-class option. The use of a class-attribute seems natural and is a well-worn pattern often used for counting instances, totalling a value across instances, or indeed limiting (usually to one, single) instantiations.


Opinion: The function attribute is most efficient, in terms of programmer time or LoC. However the class-construct seems more-readily recognisable.

That said, the OP's stated specification is to limit the depth of a stack. Many would have implemented the stack as a (derived) class, and thus adding a class-attribute control-variable would become only part of a wider whole - rather than the class being created merely to replace a simpler and more-concise function.

Contrarily, if we (all) use function-attributes and enjoy the simplicity and power justifying the reasons they were given to us, they would become second-nature to code AND to read!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to