Super Quick Python Refactoring Tips

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • In this Python Tutorial I show you another 8 quick Python refactoring tips for cleaner and more Pythonic code. This is part 2 of my refactoring code series.
    ✅ Sourcery - Free VS Code & PyCharm Extension for Refactoring: sourcery.ai/?u... *
    Refactoring video part 1: • Quick Python Refactori...
    Get my Free NumPy Handbook:
    www.python-eng...
    ⭐ Join Our Discord : / discord
    📓 ML Notebooks available on Patreon:
    / patrickloeber
    If you enjoyed this video, please subscribe to the channel:
    ▶️ : / @patloeber
    Resources: sourcery.ai/bl...
    ~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
    🖥️ Website: www.python-eng...
    🐦 Twitter - / patloeber
    ✉️ Newsletter - www.python-eng...
    📸 Instagram - / patloeber
    🦾 Discord: / discord
    ▶️ Subscribe: / @patloeber
    ~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
    🅿 Patreon - / patrickloeber
    #Python
    ----------------------------------------------------------------------------------------------------------
    This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏

Комментарии • 48

  • @patloeber
    @patloeber  2 года назад +77

    Small correction: In Tip 7 it should be - if currency in {"USD", "EUR"} - to use a set. So it must be curly braces and not parenthesis. Hope you enjoyed the video!

    • @Sabre00
      @Sabre00 2 года назад +2

      I use this all the time with a List. Will change it to a Set moving forward. Excellent video as always!

    • @BartDorlandt
      @BartDorlandt 2 года назад +1

      Isn't a tuple for effective for this. A set would only be useful when you want to enforce uniqueness. In this case a tuple is perfect.

    • @NoProblem76
      @NoProblem76 2 года назад +2

      I wonder what’s the performance difference in such a small collection between list set and tuple

    • @dariuszspiewak5624
      @dariuszspiewak5624 2 года назад

      In all earnest... There's no difference whether you use a list, a set,or a tuple here. There are just not enough elements to feel any difference. It'll start to show only when the number of elements goes into, say, hundreds or thousands.

    • @olivermohr417
      @olivermohr417 2 года назад +1

      you can also predefine standard_currencies = {"USD", "EUR"} outside the function to avoid the creation of the set each time the function is called

  • @fullstackspiderman
    @fullstackspiderman 2 года назад +13

    4:00: values are in a tuple/() not in a set/{}. Did you mean tuple or set actually?

    • @qwertical
      @qwertical Год назад +1

      You must have gotten the answer already, but I am just putting it here for the comments. It should be a set like he said, not a tuple like was shown.

    • @micexjm3730
      @micexjm3730 Месяц назад

      so the correct line is --> if currency in {"USD", "EUR"}: for a set but
      Probably both (line shown) work as well. Just remember set has unique items, tuples does not (have to)

  • @wilsvenleong96
    @wilsvenleong96 2 года назад +8

    4:00 Isn't a set in Python initialized with curly braces?

    • @patloeber
      @patloeber  2 года назад +3

      Totally right, thanks for letting me know! I pinned a comment with the correction

  • @DrDeuteron
    @DrDeuteron Месяц назад

    2:00 don’t rely on enumerate’s counter variable outside the loop, because an empty argument will lead to a NameError, as the counter assignment is never executed. I have been burned by this in the else block.

  • @Batman099
    @Batman099 2 года назад +2

    tip number 1 is not even tip. 🧐 that's how people declared list.

    • @patloeber
      @patloeber  2 года назад

      had to start with an easy one :D but sometimes you still find this in other people's code...

  • @Mdroudian
    @Mdroudian 2 года назад

    tabnine + sourcery = excellence =D

  • @krystianprogress4521
    @krystianprogress4521 2 года назад

    Thank you for this tips :)

  • @bosadam3516
    @bosadam3516 2 года назад +2

    Hi. Nice tricks
    I was trying 03:52 before I watch it :D
    currencies = ["USD", "EUR"]
    def process_payment(payment, currency):
    if currency in currencies:
    print(f"Payment: {payment} and currency is: {currency}")
    else:
    print(f"Global: {payment}")
    Thanks ^_^

  • @tigrayrimey6418
    @tigrayrimey6418 2 года назад +2

    Nice tips, thanks. could you please do some python code implementation for the Passive-aggressive algorithm for the multiclass classification case?

    • @patloeber
      @patloeber  2 года назад +1

      thanks! Will have a look at that

  • @sangchoo1201
    @sangchoo1201 2 года назад +1

    where's tip no.0?
    index not starting with 0?

  • @ulissesalvesoffsec
    @ulissesalvesoffsec 2 года назад +6

    For tip #7, I would do like this so I don't need if's:
    payment_types = {"USD": process_standard_payment, "EUR": process_international_payment}
    def process_payment(payment, currency):
    payment_types[currency](payment)

    • @sangchoo1201
      @sangchoo1201 2 года назад +2

      that's not the same code.
      video's code was
      "run standard_payment if currency is "CSD" or "EUR", otherwise run international one"
      but your code is "run different payment depend on the currency"

  • @inLofiLife
    @inLofiLife 2 года назад

    "We should be always searching for opportunities to remove duplicated code" - now I know you're like me :) thanks for the tips!

  • @MrEo89
    @MrEo89 2 года назад

    Except “is_winning” is undefined.. so assuming most people would try these in repl and find them not to work, why not come out with better example?

  • @shaishai416
    @shaishai416 Год назад

    amazing video...! please do more videos like that with refactore code, "code smell" I learn a a lot from that. and BTW the AI tool is nice... I you know more like this please let us know (:

  • @learnwithraghuIT
    @learnwithraghuIT 2 года назад

    Dam, That's good. Look forward for more such videos

  • @natnaelkahsu8708
    @natnaelkahsu8708 2 года назад

    can you make subtitles in german.

  • @kaneki_ken_07
    @kaneki_ken_07 2 года назад

    Which VS code theme don you use??

  • @toboletsgo6103
    @toboletsgo6103 2 года назад +1

    4:02 you show tuple instead of set.

    • @patloeber
      @patloeber  2 года назад

      Yeah noticed this after the video :( thanks for the hint, I already pinned a comment

  • @__________________________6910
    @__________________________6910 2 года назад +1

    I know this except 5 and 8 points.

  • @name1355_0ne
    @name1355_0ne 2 года назад

    Thanks for the video, it was useful

  • @saurrav3801
    @saurrav3801 2 года назад

    Nice video bro..🔥🔥🤙

  • @donnakillian1457
    @donnakillian1457 2 года назад

    Fascinating 😝😝😝 💖🔆🤪

  • @manishcodeshala9639
    @manishcodeshala9639 2 года назад

    great video

  • @ninjanape
    @ninjanape 2 года назад

    Thanks!

  • @ІванДмитренко-х6э
    @ІванДмитренко-х6э 2 года назад

    Tip 7. When checking if character is in string, should I convert a string into set?

    • @MrDgf97
      @MrDgf97 2 года назад +2

      Unless you’re checking a lot of characters, there’s no point. Turning a string into a set takes the same amount of time as just checking if a character exists in a string.

    • @ІванДмитренко-х6э
      @ІванДмитренко-х6э 2 года назад

      @@MrDgf97 thanks!

  • @higheloguy9057
    @higheloguy9057 2 года назад

    Tip number 1 xD? For those people that cant tie their shoes.

    • @patloeber
      @patloeber  2 года назад +1

      Gotta start with a simple one 😂

  • @fuckooo
    @fuckooo 2 года назад

    Thank you, some great tips here

    • @patloeber
      @patloeber  2 года назад

      Glad it was helpful!