-1

I came across the following code online:

class Solution: 
   def myAtoi(self, str: str):
        #function does something here

I'm not familiar with the function's second argument type str:str. What is the argument's name?
I tried to Google it, and it seems like str is the argument's name. I thought you couldn't use a type as an argument's name.
Can someone explain how it works? How would I then refer to each the variable str and the type str in my function?

user613
  • 175
  • 2
  • 10
  • 5
    It's a Python type hint. It suggests that the variable `str` should be of type `str`. Note that `str` is a very bad name for a variable though, as it clearly represents a Python type. Thus, additional references to `str` within the function definition will refer to the variable, not the type, and additional String type hints cannot be used. Python is dynamically typed, so type hints are not necessary, but they are quite useful for plugins, linters, and etc. – h0r53 Sep 23 '20 at 15:57
  • Does this answer your question? [What does -> mean in Python function definitions?](https://stackoverflow.com/questions/14379753/what-does-mean-in-python-function-definitions) – Random Davis Sep 23 '20 at 15:59
  • @RandomDavis, my question is on the argument `str:str` and not the return `->` symbol. Thanks. – user613 Sep 23 '20 at 16:10
  • @h0r53, thanks! Your comment answers my question nicely and is exactly what I wanted to know. – user613 Sep 23 '20 at 16:17
  • @user613 They are both aspects of [*function annotations*](https://docs.python.org/3/glossary.html#term-function-annotation). – chepner Sep 23 '20 at 16:33

1 Answers1

2

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 :)

Or Y
  • 2,088
  • 3
  • 16
  • Thanks! This was a very clear explanation. What is the purpose of the type before the colon if I can still pass other types to the function? Also, how do I then refer to the **type** `str` in my function, and how do I refer to the **variable** `str`? – user613 Sep 23 '20 at 16:13
  • @user613 the type before the colon is not a type but rather the name of the variable, and the reason that naming that variable `str` is a bad practice is because you have to use a workaround in order to access the builtin `str` as by default it will access the variable, ill edit my answer with more details – Or Y Sep 23 '20 at 16:18
  • @user613 one the purpose suggesting a type but not enforcing it is backwards compatibility. If possible, you should change the name of your variable `str` so that it isn't overwriting a reference to a Python keyword. All references to `str` within your function definition will reference the variable, not the type. – h0r53 Sep 23 '20 at 16:20