After switching from Python to C# I must admit that strong static typing is a life saver and don't want to go back. Dynamic programming languages are good for scripting but if you want to build serious apps you will notice that capturing silly mistakes at design time is better than capturing them at runtime.
Ok, there is literally nothing stopping you from declaring your types. I would rather go back to C++ than C#. FastAPI is competitive with .NET core not to mention ICollection its implementations make no sense. I mean, I like C# but only when I'm not using it 😂
It's for managing large projects/codebases. Because if we don't have types in python, it makes it harder to maintain or fix the code in the future. Types also tells what the code does to other programmers so they can better understand or fix the code and use it (it is also a form of docs for libraries). Also don't forget types are a neat way to debug or find errors in your code which means faster production time.
@@skiavariants1975 Thank for the describtion 🙃. But Being a dynamic programing language can be the most important feature that distinguishes Python from other languages. Why eliminate this instead of using it? On the contrary Wouldn't this make debugging harder? Edit: Sorry for my grammar fail. I'm try to improve my English :)
@@arda4030 I think that types are fine for your own use or closed packages, but when you have to build a big project with other people is when you realize is not that cool. you have to begin worrying about what other coders will do.
@@arda4030again python is still a duck typed language, nothing will stop you from passing incorrect types. Like others have said as the codebase gets larger, it's better to have more info. I find this especially useful in IDEs. Like when I import a class and call some method, the IDE let's me know what type it is returning and what types of args it is expecting. Without type hinting this could be a pain as I'll need to go to the definition and check it manually
That's not a very good example new type. Zip codes are represented by 5 numeric digit characters, or 5 num digits, a hyphen, then 4 num digits, and the first integer is always left-padded with zeroes. If the ZipCode type implemented these requirements, then it might be useful. Otherwise it's just a synonym for a native type, which only adds unnecessary complexity without adding any useful new functionality.
I would have expected an example of code that combines runtime validation and type checking: from typing import NewType # Define the NewType for ZipCode ZipCode = NewType('ZipCode', int) # Create a class to handle the validation and encapsulation class ZipCodeClass: def __init__(self, code: ZipCode): if not self._is_valid_zip(code): raise ValueError("ZipCode must be a 5-digit number") self._code = code def _is_valid_zip(self, code: int) -> bool: return 10000
Good Lord, ZIP Codes are strings, not integers. You can’t meaningfully add or subtract them, and if your ZIP Code starts with zero, you sure as heck better not drop it as you would storing it as an integer.
After switching from Python to C# I must admit that strong static typing is a life saver and don't want to go back. Dynamic programming languages are good for scripting but if you want to build serious apps you will notice that capturing silly mistakes at design time is better than capturing them at runtime.
Ok, there is literally nothing stopping you from declaring your types. I would rather go back to C++ than C#. FastAPI is competitive with .NET core not to mention ICollection its implementations make no sense. I mean, I like C# but only when I'm not using it 😂
would be great if you gave us more info, at least in the description man! What version of python are you using? 3.12? 3.13? 3.10?!
doesn't matter, it's been part of typing from the beginning pretty much
yea this has nothing to do with the changes in 3.12. This video is just poorly timed I guess
Wouldn’t it be easier to create a (data)class for this?
Going AWESOME!... and going and going...
A good type library would be able to convert bar to pascal or lbs to Newtons.
Probably you said that one anywhere in video but I couldn't notice. Why we need this in python? Or we need that?
It's for managing large projects/codebases. Because if we don't have types in python, it makes it harder to maintain or fix the code in the future. Types also tells what the code does to other programmers so they can better understand or fix the code and use it (it is also a form of docs for libraries). Also don't forget types are a neat way to debug or find errors in your code which means faster production time.
@@skiavariants1975 Thank for the describtion 🙃. But Being a dynamic programing language can be the most important feature that distinguishes Python from other languages. Why eliminate this instead of using it? On the contrary Wouldn't this make debugging harder?
Edit: Sorry for my grammar fail. I'm try to improve my English :)
@@arda4030 I think that types are fine for your own use or closed packages, but when you have to build a big project with other people is when you realize is not that cool. you have to begin worrying about what other coders will do.
@@arda4030again python is still a duck typed language, nothing will stop you from passing incorrect types. Like others have said as the codebase gets larger, it's better to have more info. I find this especially useful in IDEs. Like when I import a class and call some method, the IDE let's me know what type it is returning and what types of args it is expecting. Without type hinting this could be a pain as I'll need to go to the definition and check it manually
@@GuillermoGarcia75Thanks for your answer. I guess I understood after your answer. Thank you ❤😊
so the best way to make new types like say imaginary/complex numbers is define it as a class
That's not a very good example new type. Zip codes are represented by 5 numeric digit characters, or 5 num digits, a hyphen, then 4 num digits, and the first integer is always left-padded with zeroes. If the ZipCode type implemented these requirements, then it might be useful. Otherwise it's just a synonym for a native type, which only adds unnecessary complexity without adding any useful new functionality.
Using the type is still useful to show intent.
I would have expected an example of code that combines runtime validation and type checking:
from typing import NewType
# Define the NewType for ZipCode
ZipCode = NewType('ZipCode', int)
# Create a class to handle the validation and encapsulation
class ZipCodeClass:
def __init__(self, code: ZipCode):
if not self._is_valid_zip(code):
raise ValueError("ZipCode must be a 5-digit number")
self._code = code
def _is_valid_zip(self, code: int) -> bool:
return 10000
Thx_.
New type is only when created new class.
You forgot to talk about Newtype with object 🙄
Good Lord, ZIP Codes are strings, not integers. You can’t meaningfully add or subtract them, and if your ZIP Code starts with zero, you sure as heck better not drop it as you would storing it as an integer.
How to use torspy pip package
How to hackers spy DarkWeb?
testing