+ 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?
17 Réponses
+ 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.
+ 3
type( variable ) return the type, if you need.
Yes. Having data types makes code much readable *I think...
+ 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
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
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
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