Never saw this kind of code | Sololearn: Learn to code for FREE!

+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

4/24/2021 9:05:29 AM

Oma Falk

19 Answers

New Answer

+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

+11

isn't this a new feature in Python, which is called type-casting?

+8

https://stackoverflow.com/questions/51639332/use-of-colon-in-variable-declaration

+6

it just annotation..u can think as type hint..but i think it doesnt mean python support static type

+6

:int seems to be unnecessary. The code runs fine without them.

+5

David Ashton yes. Maybe you want read this article too https://stackoverflow.com/questions/51639332/use-of-colon-in-variable-declaration

+4

It's not a new feature ig it's been existing since python 3.7 or so. It's some kind of annotation to make variables easier to identify based on their types. I don't think the typing rules are enforced https://code.sololearn.com/cEvRgA96Iofo/?ref=app

+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

+3

Jan Markus πŸ˜‚πŸ‘

+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

+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

+2

Like declaring a,b=0,1? maybe

+2

again a typical Guido...

+2

Mirielle yes only an annotation

+1

Is it recursion

+1

It looks like defining the variable type like we do un typescript >>> let a : string;

+1

https://www.geeksforgeeks.org/top-10-python-ide-and-code-editors-in-2020/

+1

Krish [less active] its not actually defining the variable type, rather its a virtual communication between the programmers and the IDEs.

+1

Faheem Then even it's for defining the type for the ide πŸ˜‚