Lies in education [was Re: The "loop and a half"]

Neil Cerutti neilc at norwich.edu
Fri Oct 13 15:01:03 EDT 2017


On 2017-10-13, Steve D'Aprano <steve+python at pearwood.info> wrote:
> On Fri, 13 Oct 2017 11:54 pm, Gregory Ewing wrote:
>
>> Neil Cerutti wrote:
>>> I can tell at a glance if a parameter is expected to be
>>> modifiable just by looking at the function signature.
>> 
>> The question is why doesn't anyone feel the need to be
>> able to do that for Python functions? Whether a function
>> modifies things passed to it is just as important to
>> know in Python as it is in C.
>
> Lots of people would like Python to have a "freeze" function
> that can make immutable objects, it is a moderately common
> feature request.
>
> Some people (myself included) would like a "const" declaration
> that gives us names that can only be bound to once:
>
> const spam = "NOBODY expects the Spanish Inquisition!!!"  #
> Okay spam = "foo"  # Error.
>
> I don't mind if that is a runtime error, although a compile
> time error would be nicer.

When there are many constants I tend to create a class called
Constant that either doesn't get instanced, or has only one
instance (sometimes some of my constants need to be calculated
once at runtime).

> I don't know if either of these (actual immutable pure-Python
> objects, and un-rebindable names) count as quite the same thing
> you are asking Neil about. But I trust they're related.

Looking at the Python functions I actually write today, I've
evolved my style until I simply don't modify function arguments,
so it matters not if parameters are declared const.

But my oldest programs still in use are different. For example:

    records = defaultdict(Data)
    read_unmatched_records(records)

And then later code continues screwing around with that
dictionary and liberally using C-style constant names, e.g.:

    for fname in glob.glob(COUNSELING_ACK):
        process_counseling_acks(fname, records)
    for fname in glob.glob(PLUS_APP_ACK):
        process_plus_request(fname, records)

Such code feels alien to me now. A small script for reading
promissory note acknowledgements grew into a business-vital,
1,000 LOC octopus without my leave.

-- 
Neil Cerutti




More information about the Python-list mailing list