So - as it was repeated above, Python is dynamically typed. In language, this works because in fact, all in language is an object - and is an object derived from "Object".
Now, over the years, people have realized that for large projects, you have typing tips that can error as early as possible - preferably before deploy, on the types of variables, can facilitate code development and reading.
Then, in two stages: when Python 3 was created, and later on PEP 484, a syntax and helper tools have been introduced to put optional typing tips. They are called "Annotations" - an extension of the syntax that is used in conjunction with the Python "Typing" library, and external tools such as the mypy. With this it is possible to indicate the types of variables used as parameters of methods and functions, and, from Python 3.6 onwards, imitating the static language type declarations for the variables declared in the class or function body.
The Annotations are placed after the sign of :
where only the variable name appears, and the typing hint comes after that:
Ex.:
def myfunc(a: int, b: int=0) -> int:
c: int = 10
return a + b + c
In this section, I noted the parameters a, b, the variable c and the return type of the "myfunc" function with the class int
. However, this type statement has no effect while running the program. You can put any valid Python expression
after the :
, the program will work the same way. What happens is that there is a language specification (Pep 484) that details how external tools can be built that can read these statements and point out possible typing errors. But the use of these tools is completely separate from the Python compilation or execution of the program itself, and is usually done in a previous step, together with the "linter" type tools, for style checking.
This kind of statement does not impact the performance of a program in Python, but there will always be more considerations to be made. For example: annotations are available, via introspection, during the execution of the program - and it is possible yes use a tool that requires typing to be correct at runtime. The "mypy" I mentioned does not do this, but it would be perfectly feasible. Now, such a tool yes, putting extra checks on each method and function call of the program would have an impact on performance that can be significant - so the preference for static checking, before compilation.
Another fact worth mentioning is that the cython language, a super-set from Python that compiles the Python language for C (and hence for a native binary), which has been around for years, and accepts optional typing - and in Cython yes, a typing that makes a difference in runtime, could from the changes of Python 3.6 to accept the typing declaration available in Python syntax, without requiring the use of a separate syntax, and incompatible with Python for this (Cython has the keyword cdef
and others that are used to declare variables and functions optionally).
That said, if you saw code as what is in your question:
nome = str("Python")
That code is wrong. Simple as that. In Python some types of objects can be declared as "literal", that is: there is support in the syntax of the direct language for this type of object. One of these types is string: in Python, the presence of quotation marks, single or double, or the same quotation marks occurring three times: """
, without any prefix for the quotes already indicates that the object will be a string as the value that is in quotes. Call str("...")
, is simply ordering to create a string from the other string that is inside, not saying the "type" of anything.
It might even look like this, with notes, as I described earlier:
nome: str = "Python"
Python is dynamically typed. The programmer who did this that you exemplified is a pig.
– Diego Souza
how so? it is right or not to state the type of the variable?
– Lucas Olimpio
Python is dynamically typed.
– Diego Souza