Home>

If i create a method that sets a value,
I want to be able to use only strings or integers as arguments.
In that case, how should I set the type hint?

def set_value (self, column: str, value: str): # I want to be able to set not only characters but also numbers
    "" "
    Set the value.
    If the column is already set, the value is overwritten.
    "" "
    self .__ valueset [column] = value

In the case of java, we could declare as many arguments as the same method name.
I am worried that there is no way.

  • Answer # 1

    https://docs.python.org/en/3/library/typing.html#typing.Union

    from typing import Union
    def set_value (self, column: str, value: Union [str, int]):
        "" "
        Set the value.
        If the column is already set, the value is overwritten.
        "" "
        self .__ valueset [column] = value


    is.