+ 13

Never saw this kind of code

Did u ever see a kind of line 3 and 4 of that code? What is its use? https://code.sololearn.com/ca0ZTgBMDthE/?ref=app

24th Apr 2021, 9:05 AM
Oma Falk
Oma Falk - avatar
18 Answers
+ 14
It's a type annotation. It does not influence the running code but it can be good for the programmer reading that code. Some IDEs and static analyzers like mypy can use them to give you auto-suggestions and find errors. You usually put them to function declarations def sum (a: int, b: int) -> int: but you can annotate any variable. This is useful for example for empty lists arr: List[int] = [] where you cannot guess the type from the value
24th Apr 2021, 12:23 PM
michal
+ 6
it just annotation..u can think as type hint..but i think it doesnt mean python support static type
24th Apr 2021, 9:22 AM
durian
durian - avatar
+ 6
:int seems to be unnecessary. The code runs fine without them.
24th Apr 2021, 9:40 AM
David Ashton
David Ashton - avatar
24th Apr 2021, 9:43 AM
Coding Cat
Coding Cat - avatar
+ 4
Frogged / David Ashton this is actually old but rewritten trick originally created via Perl under the disguise of labelling passed to Python somewhere around 3.6
25th Apr 2021, 10:17 PM
BroFar
BroFar - avatar
+ 3
Jan Markus 😂👍
24th Apr 2021, 10:18 AM
Coding Cat
Coding Cat - avatar
+ 3
That is just an annotation. If you want to use static typing in python, then you might have a look at this, Pure Python mode in Cython | https://cython.readthedocs.io/en/latest/src/tutorial/pure.html
24th Apr 2021, 7:50 PM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 3
Js Aakash yes but the question as it relates to the topic is about specifically lines 3 and 4 where a label eg last and next are being used and identified as integers int which is not common use in python as Frogged was pointing out in her question. I did a very minor addition to her code https://code.sololearn.com/coHekpyZ6YN8/?ref=app
26th Apr 2021, 4:47 AM
BroFar
BroFar - avatar
+ 2
Like declaring a,b=0,1? maybe
24th Apr 2021, 9:15 AM
Aditya
Aditya - avatar
+ 2
again a typical Guido...
24th Apr 2021, 10:22 AM
Oma Falk
Oma Falk - avatar
+ 2
Mirielle yes only an annotation
24th Apr 2021, 4:04 PM
Oma Falk
Oma Falk - avatar
+ 1
Is it recursion
25th Apr 2021, 5:56 AM
Aman Kumar
Aman Kumar - avatar
+ 1
It looks like defining the variable type like we do un typescript >>> let a : string;
25th Apr 2021, 6:06 AM
Krish
Krish - avatar
+ 1
Krish [less active] its not actually defining the variable type, rather its a virtual communication between the programmers and the IDEs.
25th Apr 2021, 6:16 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 1
Faheem Then even it's for defining the type for the ide 😂
25th Apr 2021, 6:18 AM
Krish
Krish - avatar