This is just a hack idea I had for functions/methods that have
parameters with default values, possibly outside the control of the user
of said function, where sometimes it is desired to pass an argument to
the function that differs from the default but other times it is desired
to just use the default, whatever that may be


# An external function
def dosomething(count=5):
    doing_something


# The function using an external function
def dosomething_twice(count=None):
    # if count is none, just use the default count from dosomething
    if count is None:
        dosomething()
        dosomething()
    else:
        dosomething(count)
        dosomething(count)

    # or use a dict
    # kwargs = {}
    # if count is not None:
    #    kwargs["count"] = count
    # dosomething(*kwargs)
    # dosomething(*kwargs)


We could set the count's default value to 5 in dosomething_twice, but
then if the external function changed its default, calling
dosomething_twice with no parameters would no longer actually be using
dosomething's default.  If dosomething were in our control, we could set
count=None as default and in the code do something like count = 5 if
count is None else count.


I imagine a syntax like

def dosomething_twice(count=None):
    dosomething(count is not None ?? count) # If count is not None, pass
it, else, use the dosomething's default parameter.
    dosomething(count is not None ?? count)

This syntax is <conditional parameter test> ?? <value if test is True>. 
The requirements are that the parameter to be filled in in the calling
method must have a default value.  If the test evaluates to false, the
right hand side will not be evaluated (lazy evaluation) and instead the
default value will be used.  This can also be used for passing by
keyword arguments:

def dosomething_twice(count=None):
    dosomething(count = count is not None ?? count)
    dosomething(count = count is not None ?? count)


It could also be used to avoid specifying long argument lists:

# external
button.create(self, parent, x=0, y=0, w=64, h=32, style=...., label="",
windowclass="")


calling: ??, by itself, will fill in that parameter with the default. 
If the external code ever changes, no need to change our code to update
each time. The use case here is to skip specifying a parameter in a long
function parameter list but still have its default used.  This probably
only make sense for parameters being passed via positional arguments,
since "style=??", (pass as a keyword style the default value), can just
be done by omitting style call.


def create_buttons(style=None):
     button = Button(self, 10, 10, ??, ??, style is not None ?? style,
"Ok")  # pass the defaults for w, h, and style if a our style parameter
is None else uses the specified style, if the calling code changes the
default, our code will automatically use it.
     button = Button(self, 110, 10, ??, ??, style is not None ?? style,
"Cancel")
     button = Button(self, 110, 10, ??, ??, style is not None ?? style,
"Retry", wiindowclass=??) # see keyword "windowclass=??" doesn't make
much sense as the result is achieved by excluding it.



A similar use case could possibly be made for conditional dict assignments:

data = {}
if count is not None:
    data["count"] = count

Could be written as

data = {
    "count" = count is not None ?? count, # set the "count" key in the
dictionary conditionally, if count is not None, else don't set the key
at all
    "other_param" = ... ?? ...,
    ...
}


Thanks,

Brian Vanderburg II

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6O4XDXP2YYS4GRIO3ZDWECUK3IGFWP4R/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to