Python`s myths

I often hear from PHP or C++ developers that python hasn`t types. It is not correct.

The wikipedia says:

Smalltalk, Ruby, Python, and Self are all "strongly typed" in the sense that typing errors are prevented at runtime and they do little implicit type conversion, but these languages make no use of static type checking: the compiler does not check or enforce type constraint rules. The term duck typing is now used to describe the dynamic typing paradigm used by the languages in this group.”

So, you can't use 'str' + 1, you will give “TypeError: can only concatenate str (not "int") to str”.

Python is flexible and if we WANT we can add type casts using dunder methods (if you don't know what and why you are doing don`t ever use this).

No alt text provided for this image

Other things that you may want to know are type annotations. Since python3.9 it works fine.

No alt text provided for this image

The code editor shows warnings, but annotations are not checked at runtime.? Is this a problem?

No. We can use linters to prevent type misusing, as well as many other errors. This behavior is similar to compiling code. You can add `pylint my_progect` to the CI/CD and fail to deploy if code contains problems. Of course you can disable some warnings. For example Django requires you to disable a couple checks because Django can't check code correctly in some cases (please avoid Django if you can).

No alt text provided for this image

Python has many reasons to use duck typing. This makes python easy to write fast, short code. For small and medium projects and prototypes maybe python is the best choice. But if you want to write enterprise code - you still can use python with lints and type annotations.

要查看或添加评论,请登录

Elena Kuznetsova ????的更多文章

  • Преждевременный рефакторинг

    Преждевременный рефакторинг

    Мы достаточно много говорим про преждевременную оптимизацию, но мало про преждевременный рефакторинг и выделение…

  • Monitoring

    Monitoring

    So, our code works fine, passes all tests, and it is successfully deployed. Have we finished our work? Definitely not.

    3 条评论
  • Integration

    Integration

    We write code, test code, test user cases and then give the code to the integration - to our DevOps or for other…

  • How can you make your code/service more stable?

    How can you make your code/service more stable?

    I separate this by 3 part : Code stability. Easy integration.

    1 条评论
  • Абстракции

    Абстракции

    Строить абстракции на чистом листе это наверно самое приятное в работе программиста. Мозг вознаграждает за то что мы…

    21 条评论
  • Оценка времени

    Оценка времени

    Как часто мы сталкиваемся с "да тут пять минут" которые растягиваются на пол дня? Думаю все натыкаются на эти грабли. Я…

    7 条评论
  • Как сделать приятно разработчику?

    Как сделать приятно разработчику?

    В первую очередь выскажу свое мнение о том что пм (или любой другой канал организующий работу) не просто передатчик…

  • Давайте поговорим о когнитивных искажениях в контексте приема человека на работу.

    Давайте поговорим о когнитивных искажениях в контексте приема человека на работу.

    Когнитивные искажения это то что очень сильно влияет на нашу жизнь в любой момент времени, но особенно в моменты когда…

    2 条评论
  • Работа без лидов

    Работа без лидов

    Как выглядит самоорганизация команды и "у нас нету лидов"? А так что менеджерская работа никуда не девается. Более…

    5 条评论
  • Симптомы "ленивых, просто возраст" болячек - сборник от неврача

    Симптомы "ленивых, просто возраст" болячек - сборник от неврача

    Примечание. Это все не для того чтобы начать лечиться по интернету, это для того чтобы всполошится и начать мучать…

    2 条评论

社区洞察

其他会员也浏览了