[Python-ideas] 'Injecting' objects as function-local constants

Jan Kaliszewski zuo at chopin.edu.pl
Tue Jun 14 12:17:55 CEST 2011

Bruce Leban dixit (2011-06-13, 18:45):

> It seems to me this discussion is mixing some different issues.
> (1) having secret parameters that don't show in help().
> I think these should be thought about separately.
> *For secret parameters:* help() could have a convention that it doesn't
> display variables with that start with _ or something like that, but that

No, the idea is that after-**-constans are not only hidden-in-help-
-arguments but that they also cannot be specified/overriden in a
function call. So their usage would not be a hack that causes risk of
incidental override by caller or that makes function signatures
obfuscated (they would have to be defined separately, after **|**kwargs,
in the righmost signature part).

    def compute(num1, num2, **, MAX_CACHE_LEN=100, cache=dict()):
            return cache[(num1, num2)]
        except KeyError:
            if len(cache) >= MAX_CACHE_LEN:
            cache[(num1, num2)] = result = _compute(num1, num2)
            return result

    help(compute)                   # -> "... compute(num1, num2)"
    compute(1, 2)                   # OK
    compute(1, 2, MAX_CACHE_LEN=3)  # would raise TypeError
    compute(1, 2, cache={})         # would raise TypeError


Open question:

It's obvious that such a repetition must be prohibited (SyntaxError, at
compile time):

    def sth(my_var, **, my_var): "do something"

But in case of:

    def sth(*args, **kwargs, my_var='foo'): "do something"

-- should 'my_var' in kwargs be allowed? (it's a runtime question)
There is no real conflict here, so at the first sight I'd say: yes. 


More information about the Python-ideas mailing list