On 23 September 2016 at 13:06, Steven D'Aprano <st...@pearwood.info> wrote:
> On Thu, Sep 22, 2016 at 07:21:18PM +0000, אלעזר wrote:
>> "Expression" is something that you need its value right
>> now, and "annotation" is something that, well, annotates the code you see
>> right now.
> Right. In the case of Python, function annotations **do** have a runtime
> effect: the expressions are evaluated, and the evaluated results are
> assigned in function.__annotations__ and made available for runtime
> Don't think that function annotations are **only** for the static type
> checker. Python is a much richer language than that!
If folks are after a simple non-type-checking related example of
annotation usage, the "begins" CLI library is a decent one:
That lets you supply command line help for parameters as annotations:
In Python3, any function annotations for a parameter become the
command line option help. For example:
>>> import begin
>>> @begin.start # doctest: +SKIP
... def run(name: 'What, is your name?',
... quest: 'What, is your quest?',
... colour: 'What, is your favourite colour?'):
Will generate command help like:
usage: holygrail_py3.py [-h] -n NAME -q QUEST -c COLOUR
-h, --help show this help message and exit
-n NAME, --name NAME What, is your name?
-q QUEST, --quest QUEST
What, is your quest?
-c COLOUR, --colour COLOUR
What, is your favourite colour?
It's not a substitute for something like click or argparse when it
comes to more complex argument parsing, but it's a good example of the
kind of simple pseudo-DSL folks have long been able to create with
annotations independently of the type hinting use case.
Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/