When I pass an argument to a function, as I understand it, the function does not understand what kind of data I want to work with. The essence of the question is, can I explicitly indicate that the list / dictionary / tuple is passed to the function as an argument ... and in the function body I work with the list / dictionary / tuple ...?

Here is what I get:

def print_magicians (magicians): for magician in magicians: print(magician) def great_magicians (magicians): for i in range(len(magicians)): temp = magicians[i] magicians[i] = "Great " + temp magicians_list = ["Π“ΡƒΠ΄ΠΈΠ½ΠΈ", "ΠšΠΎΠΏΠ΅Ρ€Ρ„ΠΈΠ»ΡŒΠ΄", "Π‘Π»Π΅ΠΉΠ½"] print_magicians(magicians_list) great_magicians(magicians_list) print_magicians(magicians_list) 

Functions def great_magicians (magicians) I accept or expect any type of data as an argument (I don’t know how to put it right) and in the end I can’t work with it as a list (call its methods). And if, say, there is not a list, but a string or a number being passed in as an argument, then what happens? After C similar languages ​​in which there was a strict typification not everything is clear.

  • For example: def print_magicians(magicians: list): but this is only a hint about the type, the interpreter will not swear if another object gets there - gil9red 1:56 pm
  • And how can I make it so that I can work with an argument as with a list (call its methods)? - BeerBaron
  • Just call methods, for example magicians.append(1) - gil9red
  • After adding magicians: list as an argument, it is possible to invoke list methods. And if it is possible in more detail how this mechanism works? - BeerBaron
  • one
    In contrast to type (), the isinstance () function is specifically designed to check whether a particular class (data type) belongs to the data: b = [1,2,3] -> isinstance(b, list) -> True - S. Nick

2 answers 2

In addition to explicitly specifying the type of the argument in the function arguments, you can check the type of the argument inside the function. For example:

 def great_magicians (magicians : list): if type(magicians) is list: #Какой-Ρ‚ΠΎ ΠΊΠΎΠ΄, Ссли magicians - список else: #Какой-Ρ‚ΠΎ ΠΊΠΎΠ΄, Ссли magicians - Π½Π΅ список (Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ΄Π½ΡΡ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅) 

You can also explicitly specify the type of return value. For example:

 def great_magicians (magicians : list) -> bool: #Какой-Ρ‚ΠΎ ΠΊΠΎΠ΄ 

    You need @ functools.singledispatch

     from functools import singledispatch @singledispatch def main(args): raise TypeError("Unacceptable data type") @main.register(dict) def _(args): print("That\'s dict!", args) @main.register(list) def _(args): print("That\'s list!", args) main({1: 2}) main([1, 2]) main(10) 

    Conclusion:

     That's dict! {1: 2} Traceback (most recent call last): That's list! [1, 2] File "C:/Users/ypank/py_proj/main.py", line 21, in <module> main(10) File "C:\Users\ypank\AppData\Local\Programs\Python\Python36\lib\functools.py", line 803, in wrapper return dispatch(args[0].__class__)(*args, **kw) File "C:/Users/ypank/py_proj/main.py", line 6, in main raise TypeError("Unacceptable data type") TypeError: Unacceptable data type