I just finished looking at
https://docs.python.org/3/tutorial/controlflow.html#function-annotations
and skimming through PEP 484--Type Hints
(https://www.python.org/dev/peps/pep-0484/).  My initial impression is
that the purpose of function annotations is to enable static code
analysis tools like linters to be more effective.  Aesthetically, to
my eye it makes the function definition line more cluttered looking
and more difficult to interpret at a glance.  Of course, these are
apparently optional.  I now wonder if I should be endeavoring to add
these to my code?

It is not clear to me how to implement function annotations in all
instances.  Starting with a rather useless function:

def prt_stupid_msg():
    print('Duh!')

This takes no arguments and returns None.  While this particular
function is rather useless, many others of some utility may take no
arguments and return None.  How should these types of functions be
annotated?

What about:

def print_stuff(stuff):
    print(stuff)

Should this be annotated as:

def print_stuff(stuff: Any) -> None:

?

What about a function from the tutorial:

def make_incrementor(n):
    return lambda x: x + n

n might always be an int or might always be a float or might be
either, depending on the author's intent.  Therefore, how should n be
annotated?  In accordance with the author's intentions?  And what
about the case where n is sometimes an integer and sometimes a float?
And the return in this instance is a function.  Should the return be
annotated "function"?

And what about functions that return different types depending on
conditional statements?  How would these varying return types be
annotated?  Say:

def return_typed_value(desired_type, value):
    if desired_type = 'string':
        return str(value)
    elif desired_type = 'integer':
        return int(value)
    elif desired_type = 'float':
        return float(value)

What should I do with this?  As written "desired_type" will always be
a string, but "value" will be potentially several different types.
And the return value can be one of three types (Assuming no errors
occur as no exception handling is being done as written.).  What to do
about that?

I have more questions, but these will do for a start.  Just the long
PEP 484 document has tons of meat in it to consider, most of which is
dealing with aspects of Python I have not gotten to studying yet!


-- 
boB
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to