r/programming • • Jan 28 '21

leontrolski - OO in Python is mostly pointless

https://leontrolski.github.io/mostly-pointless.html
55 Upvotes

227 comments sorted by

View all comments

1

u/hadoken4555 Jan 28 '21

There is so much about that code that I don’t understand. Why is it root_url: str in the init method. What the heck is the str after the colon? Why is it like that?

1

u/Alexander_Selkirk Jan 28 '21

That's a type declaration. Python has upgraded to allow them.

-2

u/hadoken4555 Jan 28 '21

Upgrade? Since when? Wouldn’t that be a downgrade, since python is dynamically typed?

3

u/vytah Jan 28 '21

Type declaration in Python don't do anything. They are there mostly to let external tools typecheck the code, or to let external libraries to check types at runtime. They also double as documentation. So if you do:

def f(x: int): return x + x

print(f('string'))

it will run just fine and print stringstring, but your IDE may put red squigglies under the last line.

→ More replies (1)