10 Neat Ways To Use Underscore In Python

preview_player
Показать описание
A lot of you have probably seen an underscore being used in special ways in Python, but do you know all of them? In today's video I will be showing you 10 neat ways that you can use the underscore in Python code!

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple
00:05 Intro
00:13 Last used value
01:20 Snake-case
02:20 Number formatting
03:51 Unimportant values
05:07 Star underscore
06:09 For loops
07:22 Semiprivate variables
09:44 Name mangling
12:16 Dunder methods
15:20 Reserved names
16:15 Default case
17:56 Summing it up
Рекомендации по теме
Комментарии
Автор

Hello Bob!

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

landsgevaer
Автор

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!

troshujin
Автор

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

SebastianGuzman
Автор

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

ReynoldChua
Автор

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 =
print(f"{my_big_int:_}") # <--- note the underscore after the colon

"""
Yields:


"""

banjohead
Автор

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

syrupthesaiyanturtle
Автор

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

hitwhy
Автор

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

hedwigs
Автор

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

animesh
Автор

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

mhermovsisyan
Автор

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

mshonle
Автор

Prefixing a variable name with a single underscore actually makes it disappear from the help utility of the corresponding module, and it is also excluded from __all__

Jonathan-utn
Автор

What's the code edittor and plug in?

Tensor_Code
Автор

I can’t really see myself using the underscore to make a bigger number more readable. I’m too used to just using scientific notation.

austinburrington
Автор

Name mangling. I never realized how awkward this term is. "namangling".

Almost forgot... Hi Bob!

URNFUND
Автор

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, _

romankrt
Автор

"programmers dont know their numbers" goes hard (same here though)

sneakymilkshake
Автор

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?

tullmancraggs
Автор

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.

juschu
Автор

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

adudeandacat
welcome to shbcf.ru