-1

Given the following functions, I was advised to make the params type hints as shown in the second api_build_payload. What is the difference?

def api_build_payload(context=Context, radius_field=str, new_value=Any):

and

def api_build_payload(context: Context, radius_field: str, new_value: Any):

Thanks in advance.

fig-newtons
  • 19
  • 1
  • 1
  • 3
  • One is used for assignment `=`, and the other denotes an annotation for the variable `:` – flakes Apr 18 '22 at 20:58
  • These do two completely different things. The first create *default values* for those parameters, which almost certainly is ont what you meant. The second annotates those parameters. – juanpa.arrivillaga Apr 18 '22 at 20:58
  • Does this answer your question? [What are type hints in Python 3.5?](https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5) – flakes Apr 18 '22 at 20:59

1 Answers1

-1

The equals sign is used for supplying a default value to the argument if the caller chooses not to supply a value. This is not related to type hints. The type hint syntax is as your second example has it, with the hint coming after a colon.

def function(arg1: ArgTypeHint = defaultValue) -> ReturnTypeHint:
    pass

In your first example, rather than provide a type hint to the caller (which the program execution ignores) you are assigning the Type as the default value. This means that if the function was called without a value for each argument, the argument would have the Type as its value.

For example:

def type_test(arg1: str, arg2 = str) -> None:
  print(arg1)
  print(arg2)

>>> type_test("first", "second")
first
second
>>> type_test("Just first")  # Default value is used for arg2
Just first
<class 'str'>
ljden
  • 352
  • 1
  • 11
  • The question itself is too vague which will result in answers that aren't helpful. We should redirect OP, and future users looking for similar questions, to other resources that will answer in more detail. Answering these types of questions before narrowing down the scope ends up playing a game of "bring me a rock" with the OP, where there are many follow ups and little value for future readers. Also, this answer could simply be a comment. – flakes Apr 18 '22 at 21:12
  • Whilst the question is not well worded, I disagree that the question is vague. There is a clear misunderstanding in the question and a simple answer such as the one I gave clears up the confusion – ljden Apr 19 '22 at 03:05