[Edu-sig] the lone star parameter
kirby urner
kirby.urner at gmail.com
Thu Jan 22 02:12:51 CET 2015
Not every Pythonista knows of the lone star.
Say you want some keyword arguments that have to be mentioned, but you
would never allow them to be reached positionally.
Or: it's fine if you don't mention them, but if you know they exist, you
have powers.
def game_boot_up(pos0, pos1, *, cheat = False): # define a function
Now you can start it with:
>>> game_boot_up(1, 2) # ignore the keyword parameter (it has a default)
and all will be fine. You can't try to probe with an extra positional
"just in case":
>>> game_boot_up(1, 2, True) # anything? -- raises exception
But if you know the secret key by name...
>>> game_boot_up(1, 2, cheat = True)
Caveat: Python tends to blab what the "secret param" is in the Traceback
so this isn't really how to embed a secret cheat.
The point is to understand the role of the lone star better, a relatively
obscure corner of the language.
If the params after the lone star don't have default values then the
corresponding keyword arguments will be required.
Keyword are arguments required and yet are not pre-committed to any
defaults. Useful.
Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20150121/6e7ab2bc/attachment.html>
More information about the Edu-sig
mailing list