Typing

Asked

Viewed 425 times

0

Is it possible to use static typing in Python? For example if I declare a variable of type int and then I assign the value 'one' no error occurs, I wonder if there is any way to prevent this kind of thing.

x: int

x = 'um'
  • Buddy, you know that x:int is a note right?

  • Yes but I want to know if it has how to use to type a variable.

  • https://answall.com/search?q=python+tipagem

  • It’s just an annotation, python won’t stop you from passing a string or a list in x for example, but the IDE will issue a Warning saying that it has an annotation saying that it is int and vc is passing a string. Doc: https://docs.python.org/3/library/typing.html

  • @Matheuscezario The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

2

Directly in standard Python has no way, only can give the hint same. You can make a static analysis tool that checks the code and reports an error when you try to assign an invalid value to an annotated variable with a certain type. I may even have a tool ready from someone who does this, but I don’t know. Otherwise it has Python dialects with even static typing, for example Cython or Mypy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.