Doubt with variables

Asked

Viewed 326 times

1

Hello, I have a question, it seems silly but this getting in my way, I know that in Python you do not need to add a type to variable, example: name = str("Python") but I see some people declaring equal example above, there is a right or wrong way to declare variables in python??

  • 1

    Python is dynamically typed. The programmer who did this that you exemplified is a pig.

  • how so? it is right or not to state the type of the variable?

  • Python is dynamically typed.

3 answers

5

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" 

2

In programming we have a term called (good practice), that is, something that determines what is more or less correct, see that it does not use right or wrong, but it is what becomes more beautiful, more usual, becomes clearer for Oce and other programmers. Regarding the Python statement, it is not necessary to declare, and the code gets much cleaner by not doing. See if this article helps you: Python: Working with variables

0


This way is not wrong. The Python language gives you the freedom to choose. Including Quotes, which can be single or double, as in the example you gave. Then you can use str() or show type, without prejudice to the final result. Hug.

  • 1

    This is not the correct answer, the correct answer is jsbueno, in addition to the code str(...) be unnecessary it creates an overhead as a new str instance will be created from an object that is already a str, ie it is a waste.

Browser other questions tagged

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