Is Python strongly typed?

后端 未结 11 1374
长发绾君心
长发绾君心 2020-11-22 17:12

I\'ve come across links that say Python is a strongly typed language.

However, I thought in strongly typed languages you couldn\'t do this:

bob = 1
b         


        
11条回答
  •  一个人的身影
    2020-11-22 17:29

    TLDR;

    Python typing is Dynamic so you can change a string variable to an int

    x = 'somestring'
    x = 50
    

    Python typing is Strong so you can't merge types:

    'foo' + 3 --> TypeError: cannot concatenate 'str' and 'int' objects
    

    In weakly-typed Javascript this happens...

     'foo'+3 = 'foo3'
    

    Regarding Type Inference

    Java forces you to explicitly declare your object types

    int x = 50;
    

    Kotlin uses inference to realize it's an int

    x = 50
    

    But because both languages use static types, x can't be changed from an int. Neither language would allow a dynamic change like

    x = 50
    x = 'now a string'
    

提交回复
热议问题