+ 2

Why we don't use data type in python for declaring variable

18th Jun 2021, 11:10 AM
Sumit Kumar
Sumit Kumar - avatar
19 Réponses
+ 16
Mainly because Python is an interpreted language and there isn't really any need of having types. In a compiled language, the data type of each value must be known. Variables go on the stack in a compiled language. After putting a value on the stack, the stack pointer is offset-ed by the size of the data type (or more) so that the next value can be put. If the size is not known, the stack pointer cannot be offset. Python doesn't really have a true stack, which eliminates the need of knowing types. Also, when a data type is used, the whole code needs to be checked for type correctness before the code is executed. The basic definition of an interpreted language is that the full code is not analyzed before execution, so the types cannot be checked. This of course, is my speculation and not from any concrete source. Anyone can feel free to correct me or add any point.
18th Jun 2021, 12:07 PM
XXX
XXX - avatar
+ 11
[continued from previous answer] The only purpose of explicitly specifying data types in python is readability, which can be achieved using type annotations. Example ``` age: int = 0 name: str = "Name" ``` Read more here: https://docs.python.org/3/library/typing.html
18th Jun 2021, 12:10 PM
XXX
XXX - avatar
+ 7
Sumit Kumar , there is a technique that is named "type inference", that detects automatically the datatype of a variable during declaration / initialization. if you are interested in this you can read more about at this location: https://en.m.wikipedia.org/wiki/Type_inference
18th Jun 2021, 11:33 AM
Lothar
Lothar - avatar
+ 5
Guido decided so.
18th Jun 2021, 11:12 AM
Oma Falk
Oma Falk - avatar
+ 4
Sumit Kumar well.. Guido van Rossum is the inventor of Python. And had had to decide little things and also things that characterize the language. One of the last kind of decision was not to use data types. So my answer was short but finally correct.
18th Jun 2021, 11:44 AM
Oma Falk
Oma Falk - avatar
+ 3
Python uses objects as the core infrastructure. Sometimes typecasting with int for conversion from a string is necessary, however. It is similar to var declaration for generic objects in JavaScript.🤗
19th Jun 2021, 12:15 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Because they are reserved word and cannot be used for another function
18th Jun 2021, 9:36 PM
Mina Puker
+ 1
Mainly because Python is an interpreted language and there isn't really any need of having types. In a compiled language, the data type of each value must be known. Variables go on the stack in a compiled language. After putting a value on the stack, the stack pointer is offset-ed by the size of the data type (or more) so that the next value can be put. If the size is not known, the stack pointer cannot be offset. Python doesn't really have a true stack, which eliminates the need of knowing types. Also, when a data type is used, the whole code needs to be checked for type correctness before the code is executed. The basic definition of an interpreted language is that the full code is not analyzed before execution, so the types cannot be checked. This of course, is my speculation and not from any concrete source. Anyone can feel free to correct me or add any point The only purpose of explicitly specifying data types in python is readability, which can be achieved using type annotations. Example ``` age: int = 0 name: str = "Name" ``` Read more here: https://docs.python.org/3/library/typing.html
20th Jun 2021, 7:19 AM
Venkatesh L
+ 1
Venkatesh L Hmm.... did you just copy-pasted my answer into the same thread where I've answered?
20th Jun 2021, 8:15 AM
XXX
XXX - avatar
0
Nice answer
18th Jun 2021, 11:21 AM
Sumit Kumar
Sumit Kumar - avatar
0
Python is strongly-typed so a declaring variable's type is unnecessary. (For obvious reasons you must usually still declare variables!) Most other languages do not behave in this way and bad things can happen because of it.
20th Jun 2021, 1:11 AM
Obichukwu Ezimoha
Obichukwu Ezimoha - avatar
0
Python automatically finds the type of the variable and operations that can be performed on it based on the type of value it contains. Python is a dynamic language.
20th Jun 2021, 4:21 AM
Abhishek Kumar
Abhishek Kumar - avatar
0
Thanks
20th Jun 2021, 4:37 AM
Sumit Kumar
Sumit Kumar - avatar
0
Okay thanks
20th Jun 2021, 7:21 AM
Sumit Kumar
Sumit Kumar - avatar
0
They are reserved words. So cannot be used for another propose other from what it was reserved for.
3rd Jul 2021, 12:09 AM
Mina Puker
- 1
Thanks for your kind information
18th Jun 2021, 11:40 AM
Sumit Kumar
Sumit Kumar - avatar
- 1
Thank you
19th Jun 2021, 3:36 AM
Sumit Kumar
Sumit Kumar - avatar