What Does -≫ Mean in Python Function Definitions

What does - mean in Python function definitions?

It's a function annotation.

In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.

There's no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.

What does '- ' mean with function declaration in Python3?

This is annotation for type of return value of function.

def sum() -> expression:

That is, the parameter list can now be followed by a literal ->
and a Python expression. Like the annotations for parameters, this
expression will be evaluated when the function definition is executed.

https://www.python.org/dev/peps/pep-3107/

Python3 function definition, arrow and colon

It's just python type hinting, You can learn more in PEP 484

What Does This Python Function Argument Mean?

This function is a method of a class which takes an instance of the class (self) and a string and (should) returns an integer. (The name Atoi is ascii to integer).

The part after the Colon is a type hint, a way to make your code more friendly to the user by telling them that you expect that argument type to be str, although Python won't enforce that for you, and you can still pass other types to the function.
The same is true for the -> int part - it means that the function is expected to return an integer (And as you are the one writing it, that should be the case) but then again, Python won't enforce that this is the actual type returned from the function.

The first str, is the name of the variable which overrides the builtin type name str for the scope of the function. This is a bad practice and should be avoided. it's confusion and could lead to unexpected errors and bugs.

The reason that this is a bad practice is because it requires you to use a work-around to access the builtin type str.

Lets look on the following example:

def foo(str, a):
return str + str(a)

foo("Hello", 5)

When trying to execute that piece of code, we will receive a TypeError:

TypeError: 'str' object is not callable

Because the function would not try to access the builtin type str as in the scope of that function, the name str refers to the local variable (And it's also a pretty confusing piece of code).

If we would have want to access the builtin type str and keep the variable name str, we would have to do something like:

import builtins

def foo(str, a):
return str + builtins.str(a)

foo("Hello", 5)

Which would now work and return us: Hello5.

As you can see, the easiest way to fix that, would just be renaming the variable name to something else :)



Related Topics



Leave a reply



Submit