This way is not wrong. The Python language gives you the freedom to choose. Including the quotation marks, which can be simple or double, as in the example you gave. Then you can use str () or show the type, without prejudice to the final result. Thanks.
Then - as was repeated above, Python is dynamically typed. In language, this works because in fact, everything in language is an object - and it is an object derived from "object".
Now, over the years, staff have realized that for large projects, typing tips that can make mistakes as early as possible - preferably before deploy, on the types of variables, can facilitate the development and reading the code.
So, in two stages: when Python 3 was created, and later in PEP 484 , a syntax and ancillary tools were introduced to place optional typing tips. These are called "annotations" - an extension of the syntax that is used in conjunction with the Python typing library, and external tools such as mypy . With this you can indicate the types of variables used as parameters of methods and functions, and, from Python 3.6 , by imitating declarations of static language types for the variables declared in the class or function body.
The annotations are placed after the :
sign where only the variable name appears, and the typing tip comes after that:
Eg:
def myfunc(a: int, b: int=0) -> int:
c: int = 10
return a + b + c
In this snippet, I noted the parameters a, b, the variable c, and the return type of the "myfunc" function with class int
. However, this type declaration has no effect during the execution of the program. You can put any valid Python expression
after :
, 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 totally separate from the Python compilation or program execution itself, and is usually done in an earlier step, in conjunction with linter-type tools for style checking.
This type 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 program execution - and it is possible to use a tool that requires typing to be correct at runtime. The "mypy" I mentioned earlier does not do this, but it would be perfectly feasible. Now, such a tool, by placing extra checks on every method call and function of the program, would have a significant performance impact - hence the preference for static verification before compiling.
Another fact worth mentioning is that the cython language, a Python super-set that compiles the Python language for C (and hence a native binary), which has been around for years, and supports optional typing - and Cython Yes, a typing that makes a difference in runtime, could from the changes in Python 3.6 to accept the typing declaration available in the 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).
All this said, if you saw code like what's in your question:
nome = str("Python")
This code is wrong. That simple. In Python some types of objects can be declared as "literals", that is: there is support in direct language syntax for this type of object. One of these types is string: in Python, the presence of double or single quotation marks, or the same quotation marks occurring three times: """
, with no prefix for the quotation marks already indicates that the object will be a string as the value that is between quotation marks. Calling str("...")
, is simply ordering a string from the other string inside, not saying the "type" of anything.
It might even be like this, with annotations, as I described earlier:
nome: str = "Python"
In programming we have a term called (good practice) that is, something that determines what is more or less correct, see that I do not use right or wrong, but it is what is more beautiful, more usual, becomes clearer for you and other programmers. Regarding the statement in Python, it is not necessary to declare, and the code gets much cleaner not doing. See this article for help: insert the link description here