python not using data types for varibles | Sololearn: Learn to code for FREE!

+4

python not using data types for varibles

is it not difficult to develop projects by python when you don't use types for varibles? I mean, not knowing the types of variables or function arguments or ... makes code hard to get sometimes. like in JavaScript that we have typescript to avoid that. I think using data types really help you to understand code blocks faster. Do you agree on using data types would help? I know that sometimes not specifying type would help in some way but in my opinion knowing the data type would actually help. what's your opinion?

8/18/2022 8:20:12 PM

Elmira

17 Answers

New Answer

+8

This is an opinion based question so I do not believe this is suited for this question. You can always use your Community Feed to ask such questions. For me personally, Python is hard for me to read sometimes because it is so loosely typed, but with how widely Python is used and I'm sure it's one of the most popular programming languages, that's not something that is holding the language back at all. You either learn to adapt (including myself, cause as I said before, it's a bit hard) to it or just ignore it. There's millions of amazing programmers that use the language and they have done so much with it. So while it may be a bit harder especially coming from a more statically typed language, it has been proven it can be done.

+4

It CAN make some code hard to understand but I disagree with you... Most languages without strict typing are infact always the easiest to learn and most useful in the real world for making projects and that's what matters I will also like to add that starting from the 3 most recent versions of python, the typing annotations has been introduced and you can easily know what type of data a function expects by calling the function with the syntax name.__annotations__, this is not even possible in c++, C, Java and c# unless you read the documentations so +1 for python developers I want to clarify that typescript is just a seperate project from Javascript, it does not run on the client side and cannot be interpreted directly by the browser so we cannot say it's an alternative to javascript.. it's just a waste of time and you must convert typescript to JavaScript otherwise it won't work Lastly, opinion based questions are not supposed to be posted in the Q&A section.. I'm not sure if this rule still exists

+3

type( variable ) return the type, if you need. Yes. Having data types makes code much readable *I think...

+3

Python's annotations are not ignored at the compile time, otherwise there wouldn't be any returned informations from the func.__annotations__ attribute They're not also comment and not even similar to comments, annotations is a great tools that make code easier to read, it also provide informations for the IDEs so that library consumer will know what a function expects and what it should return, it predicate logic function and can also provide more informations about parameters All these wouldn't have happened if they're ignored at compiled time.. they maybe ignored at runtime tho

+3

To sum things up: Type annotations have more effects than just making the code more readable, e.g. for IDEs, Linters, Compiler etc. Type annotations are ignored at runtime. Both of that is true for TypeScript. So it's a fitting comparison. "THE" Documentation (as in docstrings or similar ideas in other languages) is documenting the API a module provides. Comments give devs hints about implementation details. In that sense they are documenting the code (rather than the API) and there is nothing inherently wrong or misleading in saying so. Annotations are neither "the" documentation nor comments. And last but not least: Good devs are good problem solvers. Most problems are better solved being nice to each other. So if you want to be a better dev, be friendly :D Thank you

+2

Just to demonstrate what type annotation looks like in python, similar to typescript and kotlin syntax https://code.sololearn.com/cHvv8cHUK4qt/?ref=app

+2

Mirielle Type hints don't work the same as declarations in other languages. Like for the code you provided, I changed the author variable to 6 without changing the type hint and it still ran. In Kotlin (I'm not familiar with Typescript unfortunately), it won't build/run at all cause type declarations are rules. Python, I believe, just treats type hints as suggestions or if I'm using the term correctly, syntax sugar (I may be using the wrong terminology though, sorry about that).

+2

Mirielle Thank you, I've learned something new! I did look it up and it is ignored at runtime. It's a shame they're not more standard, I haven't even learned about them in the Harvard course (it's free, anyone can do it :D) I am taking for that language. I will say though that even though you say they are not similar to comments, when looking up many articles, comments, doc strings and type-hints seems to be in Kahoots with each other, so through that research I was under the impression that they were similar, especially in the sense that they are all used for documentation. I will ask others about it more so that I respect OP's original purpose for this post.

+2

You are misunderstanding the difference between commenting and documenting a code and maybe that's the reason why you think annotations is similar to comments. Tbh, these online articles are misleading. Python has never had a multiple line comment system and using docstrings as comments is against the PEP8 commenting is for developers that have access to your source code and it must start with a #, documentations is for users but can also help developers too. Documentation according to how it is defined by the python developers can only be achieved through the docstrings and annotations. They are saved somewhere on the pvm and not ignored like comments.. they exists on the memory like a string object. This means you can use the help() function and __dir__ attributes to retrieve them

+2

It is not difficult though and we defining the data types for python Str1="This is bhadri raj" type(Str1) <class string> Integer=10 type(Integer) <class int> Float= 10.00 type(Float) <class float> ..... Compared to C,C++,java ->python is easy to develop projects

+1

Justice OP stated in the description "I mean, not knowing the types of variables or function arguments or ... Makes code hard to get sometimes" My reply is to show that type hint makes the reader to know the type of data that should be passed into a function and with such syntax similar to typescript, it'll be easier to read and understand Python's code, I've never mentioned in any part of my previous response that python ENFORCES type annotations. Pls always read to understand

+1

Mirielle You may not mean it, but the belittling is not really needed. I respect that you have much experience and a vast knowledge of this language that I wish I had for even my own favorite language but your looking down on others is not contributing to this already discombobulated Q&A section. While I have read what the OP said, I was also suggesting that type hints can also more or less still not be as helpful CAUSE they arent rules. Like I said before, since they are just suggestions, they can also make code unreadable. They're almost like comments in a sense because from my knowledge, they're ignored by the compiler at runtime. The type can be easily changed later on if someone so chooses. Just like how all classes in Python as public and can make following all 4 pillars of OOP hard to follow. My comment was not meant to detract but rather to follow up since I relate to the OP. But also like I said, countless amazing programmers, including you, have shown that it can be done without hard-fast rules.

+1

#dont use type if data := (type([1,2,3,4])) == list: print(data) #use isinstance if data := isinstance([1,2,3,4], list): print(data)

0

Wow nice

0

You can declare variables in python. It doesn't make a difference. For ex. Int x = 9 But you can't do this. Int x; x = 9

0

Isinstance is better because they better work on classes

0

You can use mypy and import typing Sequence and int