10 Neat Ways To Use Underscore In Python

Поделиться
HTML-код
  • Опубликовано: 7 ноя 2024

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

  • @landsgevaer
    @landsgevaer 4 месяца назад +36

    Hello Bob!
    BTW, in the final example with "case _", the underscore also acts like any variable, matching true to any expression and capturing it.

    • @jamesarthurkimbell
      @jamesarthurkimbell 4 месяца назад +5

      It doesn't capture in the sense of assignment to the name, e.g. you can say
      case x: return f"{x} not found"
      but if you tried something like
      case _: return f"{_} not found"
      you would get a name error

    • @BobChess
      @BobChess 4 месяца назад +3

      Hello

  • @troshujin
    @troshujin 4 месяца назад +11

    Hey, I'm loving these videos!
    I consider myself pretty good at python so I started watching to make sure I'm not missing any knowledge.
    But instead I find myself watching your videos just because I enjoy them!

    • @Indently
      @Indently  4 месяца назад

      Really appreciate it! Makes me happy to hear this :)

    • @troshujin
      @troshujin 4 месяца назад

      @@Indently Please keep making videos! I'll be eagerly waiting!

    • @aaronvegoda1907
      @aaronvegoda1907 4 месяца назад

      Same. I really enjoy these. They are concise, informative, and I find find them quite relaxing. :)

    • @nosdok74
      @nosdok74 4 месяца назад

      ditto

  • @SebastianGuzman322
    @SebastianGuzman322 4 месяца назад +2

    Hi Bob!
    Loved the vid, thanks a lot :)

  • @ReynoldChua
    @ReynoldChua 4 месяца назад +3

    Thank for the great tips. Being new to python and learning it from books, I noticed that all you variable declaration are followed by colon (Ex: values: list[int] = ...) as well as declaring the return type of functions or methods. Is this a new version of Python? Does Python enforces this like in other languages? Initially, I was confused reading the code such as "state: tuple[str, str, int] = ('Bob', 'Programmer', 27)" as most books I read don't use that format and other languages use the other way (Ex: int score = 75).

    • @dragweb7725
      @dragweb7725 4 месяца назад +7

      it is called "type annotation" or "type hint", it is a feature that is not enforced by python, the python interpreter even ignores it, but it is good practice to use it for several reasons:
      1) Dynamic code editors (pycharm, VSCode, etc...) will use them to infer variable types and warn you when type hint and value do not match
      2) the same code editors can better help you with apropriate contextual propositions when you call methods on your variables
      3) type hinting arguments in functions makes said function more self-explanatory, it is part of a good clear documentation
      4) if you install a static python type checker such as mypy, it is useful for it to work as expected

    • @callbettersaul
      @callbettersaul 4 месяца назад +1

      Type annotations were introduced in python 3.5, which was released 10 years ago (in 2014), so no, it's not a new feature.

    • @alexandertownsend5079
      @alexandertownsend5079 4 месяца назад

      The Python language has multiple versions. Which version was your book teaching?

  • @hitwhy
    @hitwhy 4 месяца назад

    hi, I like your videos and so bought all your lessons on udemy. Hope I can grasb python! Thank you for the videos!

    • @Indently
      @Indently  4 месяца назад +1

      Glad to hear you enjoy the videos! Thank you for the support! Happy learning :)

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

    very helpful

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

    Hi Bob. Awesome channel

  • @twindrawingstudio
    @twindrawingstudio 4 месяца назад +2

    Thanks you 🎉

  • @mhermovsisyan3342
    @mhermovsisyan3342 3 месяца назад

    Hello, why didn't you use type annotations when declaring self.brand?

  • @norbertdapunt1444
    @norbertdapunt1444 4 месяца назад +1

    Awesome.

  • @banjohead66
    @banjohead66 4 месяца назад +32

    I was surprised that you didn't cover this in the number formatting segment but the underscore can also be used as an f-string formatter to render large numbers:
    my_big_int: int = 1242998325235625656975629564965932645346536952465
    print(f"{my_big_int:_}") #

    • @rusektor
      @rusektor 4 месяца назад +2

      Wow, this is cool! :)

    • @banjohead66
      @banjohead66 4 месяца назад

      @@rusektor You can do a comma instead of the underscore for comma separation -- cool stuff!

    • @rusektor
      @rusektor 4 месяца назад +1

      @@banjohead66 Oh! That works! Is there also something interesting? ))) I know that I can write print(f"{num = }" instead of print(f"num = {num}") 🙂

    • @banjohead66
      @banjohead66 4 месяца назад

      @@rusektor Yeah! I use that one all the time! It's incredibly useful/convenient!

    • @portalwalker_
      @portalwalker_ 4 месяца назад +2

      ​@@rusektor He made a whole video about cool f-string syntax where he covered this number formatting and other useful tricks

  • @syrupthesaiyanturtle
    @syrupthesaiyanturtle 4 месяца назад

    what is the Self type used for and how do you use it?

  • @QBitDevs
    @QBitDevs 4 месяца назад

    What's the code edittor and plug in?

  • @kapibara2440
    @kapibara2440 4 месяца назад

    Good one!

  • @atorrres
    @atorrres 4 месяца назад +1

    Olá Bob!!!

  • @hedwig7s
    @hedwig7s 4 месяца назад +1

    By the way in the 2nd example you mixed up camelCase and PascalCase

  • @KroshkaMu
    @KroshkaMu 4 месяца назад +1

    Hello Bob!

  • @madslashers2002
    @madslashers2002 3 месяца назад

    Bob = True

  • @JuanPerez-iu9vk
    @JuanPerez-iu9vk 4 месяца назад

    TY

  • @dm_kael
    @dm_kael 4 месяца назад +1

    Hello, Bob!

  • @animesh5298
    @animesh5298 4 месяца назад +3

    Can you please also make a game in python.??

  • @Ungerlogik
    @Ungerlogik 4 месяца назад

    Hi Bob, when did you learn python?

  • @omegaroyal
    @omegaroyal 4 месяца назад

    Use _('string to translate ')

  • @BhEaN
    @BhEaN 4 месяца назад +1

    Hi Bob!

  • @mshonle
    @mshonle 4 месяца назад +2

    Thumbs up for “in modern English known as a comma”. 😂

  • @U53RN07F0UND
    @U53RN07F0UND 4 месяца назад +1

    Name mangling. I never realized how awkward this term is. "namangling".
    Almost forgot... Hi Bob!

  • @mrbutish
    @mrbutish 4 месяца назад +1

    Cool_cool_cool_😬

  • @ego-lay_atman-bay
    @ego-lay_atman-bay 4 месяца назад

    Wait, name mangling exists?

  • @zippy66666
    @zippy66666 4 месяца назад

    Gr8 m8

  • @MrSunTrope
    @MrSunTrope 4 месяца назад

    Yee, i knew it

  • @y2ksw1
    @y2ksw1 4 месяца назад +2

    English American have been bad with Numbers, too. The confusion is perfect after the million

    • @U53RN07F0UND
      @U53RN07F0UND 4 месяца назад

      I also am English American and have been very bad with Numbers, too.
      Such perfect confusion...

  • @romankrt7879
    @romankrt7879 4 месяца назад +2

    I like the underscore for tuple unpacking esp. when you only need one value from a whole bunch. As the underscore is a "throw away variable" you can use it more than once. For example you only want the profession from the following, poorly created example, you can unpack it like this:
    ("Bob", "Smith", "Developer", "Python") = _, _, profession, _

  • @avichaicohen3140
    @avichaicohen3140 4 месяца назад

    🚀💥

  • @ComradeQuestion091
    @ComradeQuestion091 4 месяца назад

    Hello Bob

  • @juschu85
    @juschu85 4 месяца назад

    A variable/function you can(/should) only use in its class OR subclass is not (semi)private, that's (semi)protected. When it's (semi)private, it also can't(/shouldn't) be used in a subclass.
    I really wonder why you don't have any actual private or protected variables/functions you CAN'T even use somewhere else even if you would try. That's pretty standard in other object oriented languages like C++, C#, Java, or PHP and I don't see a reason why Python shouldn't have this.

  • @dfields9511
    @dfields9511 3 месяца назад

    Hello bob

  • @tullman3craggs102
    @tullman3craggs102 4 месяца назад

    I'm going to come clean guys... I use snake case for
    functions eg do_this_thing and camel? For variables eg newVar, constants stay capital, anyone have a better idea to maintain readability of all the different methods/vars?

    • @U53RN07F0UND
      @U53RN07F0UND 4 месяца назад

      I generally find it best to adhere to the conventions of whichever language I'm programming in.
      So in C, snake_case_for_variables_and_functions, camelCaseForTypedefsAndStructs, and, UPPER_SNAKE_CASE_FOR_CONSTANTS_AND_MACROS.
      In Java, camelCaseForVariablesAndMethods, PascalCaseForClassesAndInterfaces, UPPER_SNAKE_CASE_FOR_CONSTANTS, and, dot.case.for.packages.
      Other devs expect these conventions to be upheld and many mistakes can be avoided by keeping your code consistent with them.

    • @mikefochtman7164
      @mikefochtman7164 4 месяца назад +1

      One project, they set the 'standard' to use camel case for vars and funcs, but for vars the first letter was always lower case. So something like:
      myAge: int = 42
      UpdateAge(currentAge: int) -> int
      return curretAge+1
      But some folks don't care so much, others get quite passionate. Use something but most important, stick with your choice.

    • @U53RN07F0UND
      @U53RN07F0UND 4 месяца назад +3

      @@mikefochtman7164 Agreed. The most important thing is that your code is consistent. FYI, the first letter of the variable being lowercase is camel-case. If the first letter is upper case along with the first letter of all other words in the variable name, it's Pascal-case.

  • @philluvschips3787
    @philluvschips3787 4 месяца назад

    If your example were in an array you would count from 0, so there would be eleven examples.

  • @adudeandacat
    @adudeandacat 4 месяца назад

    I think the only way to make anything "private" is using a decorator/wrapper, right?

    • @vlc-cosplayer
      @vlc-cosplayer 4 месяца назад +1

      There's no such thing as "private", ever! Even in languages with managed runtimes, if you find an arbitrary code execution bug, you can read any piece of memory or execute any piece of code, including those marked as private. 👀

    • @falklumo
      @falklumo 4 месяца назад

      ⁠@@vlc-cosplayerIn Java with Security Manager enabled, you‘ll have a hard time as such bugs are considered harmful and chased down quickly.

  • @orasul78
    @orasul78 4 месяца назад

    In 8-th You didn’t mention if hidden id is still accessible via self.__hidden_id inside the class definition.

    • @arnoldlayne585
      @arnoldlayne585 4 месяца назад

      It is, otherwise what would be the point? store unnecessary data?

  • @tema5002
    @tema5002 4 месяца назад

    i think you already made that video?

  • @hitaishi2222
    @hitaishi2222 4 месяца назад

    Hey bob..

  • @alvercury
    @alvercury 4 месяца назад

    How's it going, Bob?

  • @MeghdadFadaee
    @MeghdadFadaee 4 месяца назад

    Hello _!

  • @PaulPhillabaum
    @PaulPhillabaum 4 месяца назад

    Who is Bob?

  • @DrDeuteron
    @DrDeuteron 4 месяца назад

    My underscore game is weak.

  • @timurotube
    @timurotube 4 месяца назад

    what Bob did to you?)))

  • @mahdoosh1907
    @mahdoosh1907 4 месяца назад

    still no comments :( im telling this instead of "im second"

  • @michieleding1242
    @michieleding1242 4 месяца назад +1

    So i guess you dont use your brain to read 1_000_000_000_000

  • @jeffreyphipps1507
    @jeffreyphipps1507 4 месяца назад

    Hi Bob!

  • @Random-Boy_py
    @Random-Boy_py 4 месяца назад +2

    Hi Bob!