Video není dostupné.
Omlouváme se.

10 Ways You Can Use "_" In Python (Do you know ALL of them?)

Sdílet
Vložit
  • čas přidán 25. 07. 2024
  • Here are 10 ways you can use the underscore (_) in Python when programming. Did you know all of them?
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels
    00:00 Intro
    00:05 1) Last used value
    00:49 2) Number formatting
    01:27 3) Unimportant values
    02:17 4) Unimportant exceptions
    02:56 5) Unpacking
    03:37 6) Super unpacking
    04:16 7) Protected
    05:46 8) Private
    06:41 9) Reserved names
    07:17 10) Dunder methods
    09:26 11) Naming variables
    09:34 Conclusion

Komentáře • 59

  • @aflous
    @aflous Před rokem +61

    "as _" ? Why the h*** would you ever do that? Just delete those 4 extra characters from you code

    • @thegothaur
      @thegothaur Před 5 měsíci

      it's used by django's translate engine

  • @ilcp331
    @ilcp331 Před rokem +14

    My Mom: What should we name him?
    My Dad: Name him "_", we do not care about him

    • @shreyasj6437
      @shreyasj6437 Před 4 měsíci +1

      In C# it is literally called the discard character

  • @garybigden810
    @garybigden810 Před rokem +8

    Great presentation style and hugely memorable content! Thanks Buddy.

  • @Sinke_100
    @Sinke_100 Před rokem +9

    In try except if you need to use _ you don't need it in the first place, just try except without variable output.
    Otherwise, great video man, this one was useful and informative 👍🏻

  • @callbettersaul
    @callbettersaul Před rokem +37

    2:32 But if you don't need to use the exception, why even write "as _" instead of leaving it to be "except ZeroDivisionError:"?

    • @Indently
      @Indently  Před rokem +13

      From what I read and saw, some people use it to explicitly show that they are ignoring it, but it is probably a very rare use case.

    • @richardneumann3335
      @richardneumann3335 Před 11 měsíci

      @@Indently I am glad I never saw that. I'd be confused. If I explicitly want to ignore an exception, I'd just leave the assignment out. And if I don't even want to handle it, I'd use contextlib.suppress().

    • @vinylSummer
      @vinylSummer Před 2 měsíci

      ​@@Indently well, if one wants to suppress it, with contextlib.suppress() is a much cleaner option

  • @loverboykimi
    @loverboykimi Před rokem

    Thats very good. Looking forward to dunder methods of class video.

  • @t.e.k.profitstraders8796
    @t.e.k.profitstraders8796 Před 4 měsíci

    I think some of it was refreshing, duunder methods, however there were pretty neat tricks there!! We'll certainly be back again!!

  • @finnthirud
    @finnthirud Před rokem

    I enjoy your videos with your personal touch.

  • @lukerichards22
    @lukerichards22 Před rokem +2

    What's the benefit to using it in exception handling, when you don't need to catch what the exception is at all if you're not gonna use it? Am I missing anything?

  • @murphygreen8484
    @murphygreen8484 Před rokem

    Unimportant exceptions was a new one for me!

  • @MetaaR
    @MetaaR Před 4 měsíci +2

    You forgot about 'case _:' in a match expression.
    Example:
    n = int(input())
    match n:
    case 1:
    print("n is 1")
    case 2:
    print("n is 2 times more than 1")
    case 3:
    print("n is bigger than 1")
    case _:
    print("n is weird")

    • @user-iy6dt4xp5o
      @user-iy6dt4xp5o Před 3 měsíci

      Here, _ is a wildcard that never fails to match. If none of the other cases match, it will match with case _.

  • @gravity9999
    @gravity9999 Před rokem

    What text editor/IDE you are using

  • @luissantos975
    @luissantos975 Před rokem

    Wonderfull!

  • @mayorc
    @mayorc Před rokem

    Some are pretty useful.

  • @DealazonDaily
    @DealazonDaily Před rokem

    How can we connect Cypress to python in pycharm? Can you please tell me

  • @intron9
    @intron9 Před 4 měsíci

    6:48 a more common reserved name that would be wanted to use it for something else is "id" , it let's you reassign it though, but it's not recommended .

  • @DamjanDimitrioski
    @DamjanDimitrioski Před 3 měsíci

    Can you cover setting _ (underscore) instead of method parameters in the signature some method/function?
    Uses cases: the IDE or linter complains the parameter is not used, but you don't need it and the specification states you must put it there, and also it doesn't support optional params and other neat features and it's in args section so no kwargs will work.

  • @daggerhound1395
    @daggerhound1395 Před rokem +3

    U could do user._User__get_id() at 6:35 if any1 was curious

  • @oida10000
    @oida10000 Před rokem +3

    To point 4, it is also default case in the new match structure (3.10 and up).
    match type(myvariable):
    case type(int()):
    do_int_staff(myvariable)
    case type(float()):
    do_float_stuff(myvariable)
    case type(str()):
    do_string_stuff(myvariable)
    case _:
    print(f"Unsupported type {_}")
    Update: someone explains to me why this code throws an error in line case type(int()) but print(type(int())) works as expected.

    • @thomaseb97
      @thomaseb97 Před rokem +1

      match case is pattern matching, you're using type(int()) you should just use int()

    • @schlopping
      @schlopping Před 11 měsíci

      Is that not just a naming convention? Doing `case unused_variable:` would do the same thing

    • @sangchoo1201
      @sangchoo1201 Před 11 měsíci

      just
      case int:
      doesn't work..?

    • @schlopping
      @schlopping Před 11 měsíci

      @@sangchoo1201 case int: would compare it to literally the int type, instead of checking if its an instance of int or not. its supposed to be case int():

    • @sangchoo1201
      @sangchoo1201 Před 11 měsíci

      @schloppppdev3002
      1.
      match type(myvariable):
      wants to check the variable's type
      2.
      match int():
      is actually checking the variable matchs 0
      (because default constructor of int creates 0)

  • @rishiraj2548
    @rishiraj2548 Před rokem +1

    Thanks

  • @samadshaik
    @samadshaik Před rokem +1

    what IDE do you use??

  • @HorridModz
    @HorridModz Před rokem +2

    For 7 (Private) and 8 (Protected) - 4:16-6:40, I believe you are mistaken. AFAIK, both protected and private methods use a single underscore by convention, and name mangling with double underscore is not correct. Though both are common, I believe name mangling is incorrect. I don't know how to verify this, though.

    • @Indently
      @Indently  Před rokem +2

      I don't know where you're getting your information from. What I shared is a very common convention in Python, if you want to learn more about name mangling, check out mCoding, he made a proper video on it.
      Otherwise if you have resources that prove otherwise, I wouldn't mind looking at them.

    • @user-vt9bp2ei1w
      @user-vt9bp2ei1w Před rokem +4

      @@Indently The name mangling is to avoid subclasses accidentally overriding parent class methods when inheriting.
      This behavior is documented in the wiki (and pep-0008): "Python's runtime does not restrict access to such attributes, the mangling only prevents name collisions if a derived class defines an attribute with the same name."

    • @ommahajan1
      @ommahajan1 Před rokem

      ​@@user-vt9bp2ei1w Yes I agree, the function does not become private neither it becomes difficult to find its name.
      I tried this by creating a sample class with a double underscore leading function. Then I passed in the object created by that function in dir() global function which had the function inside my class named as: '_ClassName__functionName'.
      According to PEP 8 :
      __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo)

  • @gavintillman1884
    @gavintillman1884 Před rokem

    I’ve come across both * and _ in an unpacking context, but never *_ together. Is that quite new?

    • @Indently
      @Indently  Před rokem +1

      It's been around for as long as I can remember :)

    • @ali-g
      @ali-g Před rokem +1

      "*" usually means all so you are unpackaging all the in between values to "_"

    • @gavintillman1884
      @gavintillman1884 Před rokem +3

      I’m overanalysing it. It’s just treating _ as a variable and doing the usual * unpacking. So *_ isn’t a Python symbol, it’s just the * unpacking operator applied to variable _. The whole _ is just a convention that it’s ignored, syntactically (outside of console mode) it’s just another variable - right?

    • @ali-g
      @ali-g Před rokem

      @@gavintillman1884 Yes exactly, if you want you can even use emojis instead of _. "*" operator changes it's behaviour if it's in between int or float data types; acts as a multiplier. If left side is a string and right side is an int, acts as a repeater. I think these two the most common ones.

  • @alexislayton2150
    @alexislayton2150 Před rokem +1

    Why does this video not mention the important role _ plays is match patterns?

    • @Indently
      @Indently  Před rokem +3

      Because I absolutely forgot about it until you mentioned it! damn, it will be for the next one

    • @yoverale
      @yoverale Před 4 měsíci

      @@Indently you forgot to mention one another common use you are even using in the video, defining __init__ method for classes 7:53 anyway great video as usual!

  • @lukerichards22
    @lukerichards22 Před rokem

    Why use __str__() instead of __repr__()?

    • @sangchoo1201
      @sangchoo1201 Před 11 měsíci +1

      __str__ is to convert the instance to a string
      __repr__ is to represent the state of an instance (more close to debugging stuff)

  • @mhm6421
    @mhm6421 Před rokem +2

    Everyone asks what is an underscore but no one asks how is the underscore...

  • @sangchoo1201
    @sangchoo1201 Před 11 měsíci +2

    _ is just a variable name.
    with a context of "I don't use it"

    • @eitantal726
      @eitantal726 Před 4 měsíci

      Not exactly. open python console, type '10', enter. Then type '_' , enter. you'll see 10. More than "just a variable name"

    • @sangchoo1201
      @sangchoo1201 Před 4 měsíci

      @@eitantal726 but it's still just a variable name, although the referenced variable is a special variable

    • @eitantal726
      @eitantal726 Před 4 měsíci

      @@sangchoo1201 no, "Eitan" is just a variable name. when I type 10 and Eitan, I don't see 10. _ is different. I see it as "ANS" of the calculator

    • @sangchoo1201
      @sangchoo1201 Před 4 měsíci

      @@eitantal726 but _ IS a variable name, isn't it?
      what is a variable name:
      sangchoo1201
      _
      input
      what is NOT a variable name:
      if
      @#$
      1a

  • @HononoSNK
    @HononoSNK Před 11 měsíci

    Basically, underscore is just variable name or simple letter. Nothing special. In can be replaced with any other letter.

  • @aimanshaa
    @aimanshaa Před rokem

    Update you IDE