newb question about @property

Ian Kelly ian.g.kelly at gmail.com
Wed Oct 4 09:41:44 EDT 2017


On Wed, Oct 4, 2017 at 5:07 AM, bartc <bc at freeuk.com> wrote:
> It is just being elitist. I have a preference for keeping things simple and
> avoiding unnecessary complexity. But with programming languages many do have
> a penchant for the latter.
>
> As an example, a recent discussion on comp.lang.c was about implementing
> named constants. I proposed one very simple way of doing it, other people
> were talking about using #define, enum, const, static const, or importing
> constexpr and special rules for 'const' from C++. All unsatisfactory and
> each having their own problems.
>
> For that matter, I don't think Python has such a feature either. So that you
> write for example:
>
>   const C = 123345
>
> and then whenever C appears within the code, it's implemented as:
>
>   LOAD_CONST (123345)
>
> I'm pretty sure that there are very complicated ways of achieving something
> similar, maybe with all your decorators, or using PyMacro or whatever. But
> not doing it straightforwardly. [Python's design makes a simple
> implementation harder.]

Python has the simplest named constants of all:

C = 12345

As long as you don't subsequently change it, it's a constant. And it's
very simple because it works just like any other variable.

Python also has a particularly flexible Enum implementation, but if
you don't want it then don't use it.

>> You don't think that adding a cache for an expensive function is
>> programming?
>>
>> If you had ten expensive functions, and you wanted to add a cache to each
>> of
>> them, would you write out ten separate caches (probably copying and
>> pasting
>> the code each time)?
>>
>> Or would you write a function that builds a cache and adds it to the
>> expensive
>> function *once*, then call it ten times, once for each function being
>> wrapped?
>
>
> I'm trying to think of a real example where I've had to add a cache to to a
> function, whatever that even means (memoisation?).

You've never used a dynamic programming algorithm?

>> That you think that this is not programming is an indictment off your
>> programming skills. These sorts of functional programming techniques go
>> back
>> to the 1950s, Lisp is literally the second oldest high-level language ever
>> (only Fortran is older). You may or may not have been a hotshot in your
>> little corner of the programming world, but there's an entire world out
>> there.
>
>
> What other languages apart from Python have equivalent features to
> decorators and, what's the other one, descriptors? Apart from Lisp.

Literally any language with first-class function types, whether they
have the @decorator-style syntactic sugar for it or not.

Descriptors are a bit unique to Python, I'll grant, but mostly they're
just used in the form of properties. Here's a list of languages that
support properties:
https://en.wikipedia.org/wiki/Property_(programming)#Example_syntax



More information about the Python-list mailing list