Create Custom New Types in Python

Поділитися
Вставка
  • Опубліковано 30 січ 2025

КОМЕНТАРІ • 24

  • @agailloty
    @agailloty 8 місяців тому +5

    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.

    • @superuser8636
      @superuser8636 8 місяців тому

      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 😂

  • @MrEo89
    @MrEo89 8 місяців тому +1

    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?!

    • @trag1czny
      @trag1czny 8 місяців тому

      doesn't matter, it's been part of typing from the beginning pretty much

    • @crixi__
      @crixi__ 8 місяців тому

      yea this has nothing to do with the changes in 3.12. This video is just poorly timed I guess

  • @arnoudstolk5744
    @arnoudstolk5744 8 місяців тому +1

    Wouldn’t it be easier to create a (data)class for this?

  • @GuillermoGarcia75
    @GuillermoGarcia75 8 місяців тому

    Going AWESOME!... and going and going...

  • @pnachtwey
    @pnachtwey 8 місяців тому +1

    A good type library would be able to convert bar to pascal or lbs to Newtons.

  • @arda4030
    @arda4030 8 місяців тому

    Probably you said that one anywhere in video but I couldn't notice. Why we need this in python? Or we need that?

    • @skiavariants1975
      @skiavariants1975 8 місяців тому +2

      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.

    • @arda4030
      @arda4030 8 місяців тому

      @@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 :)

    • @GuillermoGarcia75
      @GuillermoGarcia75 8 місяців тому

      @@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.

    • @pranavp.a1200
      @pranavp.a1200 8 місяців тому

      ​@@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

    • @arda4030
      @arda4030 8 місяців тому

      ​@@GuillermoGarcia75Thanks for your answer. I guess I understood after your answer. Thank you ❤😊

  • @vishnubalaji9500
    @vishnubalaji9500 8 місяців тому

    so the best way to make new types like say imaginary/complex numbers is define it as a class

  • @IronTeddyBear
    @IronTeddyBear 8 місяців тому +11

    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.

    • @kellymoses8566
      @kellymoses8566 8 місяців тому

      Using the type is still useful to show intent.

  • @EliSpizzichino
    @EliSpizzichino 8 місяців тому

    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

  • @philtoa334
    @philtoa334 8 місяців тому

    Thx_.

  • @zj7498
    @zj7498 8 місяців тому

    New type is only when created new class.

  • @kmano2915
    @kmano2915 8 місяців тому

    You forgot to talk about Newtype with object 🙄

  • @NickDanger3
    @NickDanger3 8 місяців тому

    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.

  • @animelover5849
    @animelover5849 8 місяців тому

    How to use torspy pip package
    How to hackers spy DarkWeb?

  • @Keepkip
    @Keepkip 8 місяців тому

    testing