Don't Write Code Like THIS In Python 😡

  Рет қаралды 107,715

Indently

Indently

Жыл бұрын

Please always remember this when you're coding in #Python. #Shorts

Пікірлер: 180
@josemata8865
@josemata8865 Жыл бұрын
What about "if user:" or "if not user" for these kind of condition?
@xE92vD
@xE92vD Жыл бұрын
if not user: would be the one if you wanted to do something if user is empty, None, etc. Since None, null, empty string is all False.
@fumano2679
@fumano2679 Жыл бұрын
if not user isnt same because it will also trigger when user is an empty strong, list, dict, ... and also if user == False
@333HaKan333
@333HaKan333 Жыл бұрын
If user != “”
@doltramir
@doltramir Жыл бұрын
@@fumano2679 and if it's 0, 0.0, 0j (int, float and complex respectively)
@doltramir
@doltramir Жыл бұрын
@@333HaKan333 bad idea. "hello" != "" - True None != "" - also True if string is not None: ... - is the best way here.
@jonhjorturbrjansson9052
@jonhjorturbrjansson9052 Жыл бұрын
How about just 'if user:'?
@not_vinkami
@not_vinkami Жыл бұрын
This can be a little bit unclear, but it works
@_sabritos
@_sabritos Жыл бұрын
@@not_vinkami Not at all cases : if the Node equal to 0 it would be False not True
@Nofxthepirate
@Nofxthepirate Жыл бұрын
This is usually only used when the condition variable is a boolean because the behavior becomes less clear with other data types
@BboyKeny
@BboyKeny Жыл бұрын
@@Nofxthepirate if not not user: Now it's a boolean for sure
@nyther
@nyther Жыл бұрын
"if user" would return True because the string would be evaluated by its lenght, which in this case is above 0
@TheDutchisGaming
@TheDutchisGaming Жыл бұрын
Love how the third variant already has squiggles noting something irregular probably.
@Indently
@Indently Жыл бұрын
Real men ignore the squiggles
@grubbygeorge2117
@grubbygeorge2117 Жыл бұрын
@@Indently that’s why women are better at coding. Never ignore the squiggles!!
@DaniPaunov
@DaniPaunov Жыл бұрын
There seem to be a fair number of comments about a few things 1. Why not "if user" I'm fairly certain that can give you false without user being none 2. What about "if user != None" checking for equality with None is generally not good practice. furthermore, from what I've read, it's possible for classes to redefine that to have special behaviour in both cases, it doesn't matter whether the code is shorter, it matters whether it's readable. a lot of people are likely to find those less readable.
@opus_X
@opus_X Жыл бұрын
In what scenario does "if user" give false even if the string isn't empty.
@zeldaplayergl11
@zeldaplayergl11 Жыл бұрын
If user: will always be true as long as there is a value. Whether user is None or a Falsy value, you'll most likely handle it the same way. So it's often not a problem
@StephenRayner
@StephenRayner Жыл бұрын
Only the first one is acceptable. And you should invert it and return or throw if user is None to handle the incorrect usage.
@tb15900
@tb15900 Жыл бұрын
Don’t use number 2 either. Stick to convention, you’ll see the first in codebase everywhere; readability and speed of understanding is key
@doltramir
@doltramir Жыл бұрын
if not x is y -- is two operations: logical 'is', and logical 'not'. Same idea as if you did this: if not x == y if x is not y -- is one operation: logical 'is not' Again, same idea as: if x != y Get rid of redundant 'not'.
@jkljosh7392
@jkljosh7392 Жыл бұрын
I feel like you'd instead check if the user is None then print a error and stop the code instead of only running code if it is
@QckSGaming
@QckSGaming Жыл бұрын
Here's the coder that got past junior!
@deidarasan2760
@deidarasan2760 Жыл бұрын
Lol second one killed me
@ryanzwe
@ryanzwe Жыл бұрын
It's a PowerShell standard
@SP-db6sh
@SP-db6sh Жыл бұрын
Plz Make a series on PEP guidelines & refactoring with custom pylint setup ! is it possible to customise pylint & change code automatically if error detected ?
@Indently
@Indently Жыл бұрын
I’ll look into it! I haven’t eBen thought about a spellcheck for coding, usually because I think that if anything is done automatically, that can lead to some very well hidden bugs in our code that we wouldn’t even notice. I’ll do some research on it though.
@nathanhessling7841
@nathanhessling7841 Жыл бұрын
use if user: in this case, it also protects against empty strings, dicts, lists, sets, etc. only time not to do it this way is if working with ints where 0 is valid because 0 is falsy. that is if you want a non-falsy value.
@NoProblem76
@NoProblem76 Жыл бұрын
Google says "if not user" is preferred when possible. Unless explicit check is required
@lengors7327
@lengors7327 Жыл бұрын
"if not user" isn't equivalent to "if user is not None" since the former evaluates to true when user is none, but also something with a length of 0 (dicts, lists, etc) or false or 0, while the latter only evaluates to true if user is none and only none.
@grubbygeorge2117
@grubbygeorge2117 Жыл бұрын
@@lengors7327 Isn’t evaluating to false when the user is empty the preferred outcome most of the time? You wouldn’t want to put empty user names in your database
@lengors7327
@lengors7327 Жыл бұрын
@@grubbygeorge2117 that scenario happens only when you're first receiving input from the user in which case user will never be None and will always be a string. In that case you're better of testing the length of the string since, most of the time, you actually want something thats larger than 2/3 characters... when dealing with databases, tipically (although not always) user would only be none when querying it (for a single user) and the database doesnt find any match in which case you get none. In this case you either receive an user object or none. So checking for none is enough... but yes, you could also simply use "if not user"/"if user", I wasnt saying that you should never use that sort of check, just that its different from checking "is none" and that you should know the difference or you might, unintenionally, introduce bugs in your code
@imperial2069
@imperial2069 Жыл бұрын
Ok so ill do my best to confuse people reading my code , thx for this advice
@rajveersingh-ix6ti
@rajveersingh-ix6ti Жыл бұрын
this is on a different level. > user = "Mark" > user and print(user)
@denizsincar29
@denizsincar29 Жыл бұрын
myfunction that returns true for success and false on error: (get_file('url') and print('success')) or print('error')
@hackerulroman
@hackerulroman Жыл бұрын
what happens when we use 100% of our brains:
@denizsincar29
@denizsincar29 Жыл бұрын
@@richardpaulhall it is only awailable with dynamicly typed langs.
@jambonmusical2689
@jambonmusical2689 Жыл бұрын
Ok, Always go for what will bother people the most, got it
@gabrielmaimonie7005
@gabrielmaimonie7005 Жыл бұрын
Thank you
@drrenard1277
@drrenard1277 Жыл бұрын
I do believe that python allows that for those programming in other national languages where subject, verb, noun, etc are not in same order as English.
@fluffykitties9020
@fluffykitties9020 Жыл бұрын
😮 in python, if a string(or any object) is empty it evaluates to None, which python considers falsy". so all you need is.. >>> if user: which will return False if the str, or list, or dict is empty.🎉
@Indently
@Indently Жыл бұрын
That is False, it does not evaluate to None, it evaluates to False
@fluffykitties9020
@fluffykitties9020 Жыл бұрын
@@Indently that part of my answer is incorrect, thanks for the correction, but my answer is stll correct.
@siddharth4838
@siddharth4838 Жыл бұрын
Thank you! Loving your videos!
@xxlarrytfvwxx9531
@xxlarrytfvwxx9531 Жыл бұрын
My VSCode never yelled at me about this.
@badonker
@badonker Жыл бұрын
I refuse to think that people use 3rd variant pretty much unreadable
@peckychicken
@peckychicken Жыл бұрын
What about if user not is none?
@ben_jammin242
@ben_jammin242 Жыл бұрын
I think old school programmers would prefer if !(user == None) or if (user != None) over if (None != User). Either way, that's a bad way to check for empty strings. Afaik an empty string is still a string, it's not None...
@hectormoreno-bravo8399
@hectormoreno-bravo8399 Жыл бұрын
What is MOST READABLE
@dc22199x
@dc22199x Жыл бұрын
The first if is normal, the 2nd one is called Yoda condition as in like master yoda in star wars, the 3rd one no one that I know uses that especially in opensource community 😅
@Indently
@Indently Жыл бұрын
I just took the 3rd one right from the PEP documentation for things you should never do 😂
@joergsonnenberger6836
@joergsonnenberger6836 Жыл бұрын
The only reason the third way works is because it is actually parsed as "not (a is None)" instead of "(not a) is None". Given that most Python programmers won't know the operator precedence between "not" and "is" by heart, that's a no-go.
@gbpekalp
@gbpekalp Жыл бұрын
What is this font
@kamranview9465
@kamranview9465 Жыл бұрын
Love you brother
@Nofxthepirate
@Nofxthepirate Жыл бұрын
This reminds me of the time I learned you can reference array elements in C by saying index[array] instead of array[index]
@joergsonnenberger6836
@joergsonnenberger6836 Жыл бұрын
That's an important code golf technique, because you can use the operand inside [] for assignments without any extra spaces :), e.g. "1[a=b]".
@dhyan_a
@dhyan_a Жыл бұрын
I have a doubt what if we do if user != ' ': print(user) Same output, but is this a bad practice?
@ntlake
@ntlake Жыл бұрын
Just use: if not user:... For the cases (like this one) where the user can't be a falsy value like 0 or "".
@zeldaplayergl11
@zeldaplayergl11 Жыл бұрын
Me: If not user:
@SurCreVlad
@SurCreVlad Жыл бұрын
C guys at the same time: if (user) printf("%s", user);
@chrislbaird
@chrislbaird Жыл бұрын
Is none the same as null?
@officialacescottie
@officialacescottie Жыл бұрын
If user not in ['', None, "None"] Or just use regex
@LuvxJacqu4li8e
@LuvxJacqu4li8e Жыл бұрын
The power of python
@franmedina2096
@franmedina2096 Жыл бұрын
Why not “if user:”, much simpler
@nineteenn7866
@nineteenn7866 Жыл бұрын
“if user:” is readable to me
@VelocityWIS
@VelocityWIS Жыл бұрын
Thanks
@zxw
@zxw Жыл бұрын
if user: print(user) ?
@Indently
@Indently Жыл бұрын
That is clean, but shouldn't be mixed up with explicitly checking if something is not None.
@artemetra3262
@artemetra3262 Жыл бұрын
this would also include the case when user = '' which is *not* None
@Indently
@Indently Жыл бұрын
​@@artemetra3262 With not None, we're looking that the variable is explicitly not None, it doesn't matter if it's empty, that's a different thing. I could have picked a better example for sure though, oh well, live and learn. "" is not None returns True None is not None returns False But the video is not about that ahah. It's really about how you should be formulating your logic...
@artemetra3262
@artemetra3262 Жыл бұрын
@@Indently yes, i know that, which is why i said that the method provided by John is not the same as an explicit comparison to None
@Indently
@Indently Жыл бұрын
​@@artemetra3262 Ah, I couldn't tell what you meant ahah, then just ignore the paragraph I wrote 😅
@tarunshetty3434
@tarunshetty3434 Жыл бұрын
which editor is this?
@Dalmen
@Dalmen Жыл бұрын
PyCharm
@dvoltaire
@dvoltaire Жыл бұрын
How about if user: print(user)
@frankv0
@frankv0 Жыл бұрын
User isn’t a boolean so it would just give an error
@fortznite8150
@fortznite8150 Жыл бұрын
@@frankv0 what do you mean, it doesnt give any error?
@frankv0
@frankv0 Жыл бұрын
@@fortznite8150 yea I realized that after I commented it lol
@junkynioy
@junkynioy Жыл бұрын
Baby yoda:
@abhijithnt7177
@abhijithnt7177 Жыл бұрын
An empty string in a variable means Fasle.
@altairbueno5637
@altairbueno5637 Жыл бұрын
If user usually does the thing
@snopz
@snopz Жыл бұрын
if user != None: print(user)
@rbxneko
@rbxneko Жыл бұрын
How about "user != None"?
@CeciEstUnIdentifiantYouTube
@CeciEstUnIdentifiantYouTube Жыл бұрын
And what about "if user!=None :" it works right ?
@Indently
@Indently Жыл бұрын
No that's wrong, you want to compare the instance, not the value, that's why you use "is".
@joergsonnenberger6836
@joergsonnenberger6836 Жыл бұрын
If user is a class that provides __eq__, all bets are off.
@mikhailmiller9262
@mikhailmiller9262 Жыл бұрын
Why not just: if user: print(user)
@kamranview9465
@kamranview9465 Жыл бұрын
Thanks for sharing this information
@gtxg.
@gtxg. Жыл бұрын
Just do if user:
@FirstNameLastName-gh9iw
@FirstNameLastName-gh9iw Жыл бұрын
I’m definitely weirded out by python syntax, is not is weird, and the for loops are the worst
@CrjaseMechaEngr
@CrjaseMechaEngr Жыл бұрын
Or just “if not user”
@Indently
@Indently Жыл бұрын
You'd think so at first, but that's actually a very common mistake.
@lakshraja4831
@lakshraja4831 Жыл бұрын
This mostly happens when you learn other languages like of the c family where it is written as != Null. Plus for us it is more readable but we forgot that there are python programmers who cry over such small things.
@Indently
@Indently Жыл бұрын
Python programmers cry over evvveeeerrryyything 😂
@lakshraja4831
@lakshraja4831 Жыл бұрын
@@Indently lol
@imrannooraddin1876
@imrannooraddin1876 Жыл бұрын
better advice: use the assert keyword in a try except clause. try: user = input() assert user is not none # rest of code except AssertionError: #handle the error
@adiveler
@adiveler Жыл бұрын
if Baba is not you:
@AkivaB
@AkivaB 11 ай бұрын
I just use if user:
@dcknature
@dcknature Жыл бұрын
if user is not None: makes the most sense to me. Other variants are good for Yoda. Size matters not. Look at me. Judge me by my size, do you? Hmm? Hmm. And well you should not. For my ally is the Force, and a powerful ally it is. Life creates it, makes it grow. Its energy surrounds us and binds us. Luminous beings are we, not this crude matter. You must feel the Force around you; here, between you, me, the tree, the rock, everywhere, yes. Even between the land and the ship. YODA, TO LUKE SKYWALKER
@Indently
@Indently Жыл бұрын
😂
@sniperdaoud
@sniperdaoud Жыл бұрын
if user None is not 🤔
@V1kToo
@V1kToo Жыл бұрын
if user: print(user)
@SirenGlitch
@SirenGlitch Жыл бұрын
If user !None:
@Dalmen
@Dalmen Жыл бұрын
this will not work. my you mean !=
@shlokbhakta2893
@shlokbhakta2893 Жыл бұрын
If user is not not user
@Dalmen
@Dalmen Жыл бұрын
Syntax Error!
@jasonmnayerji1783
@jasonmnayerji1783 Жыл бұрын
Isn't it simpler to say if user: Condition
@jack_papel
@jack_papel Жыл бұрын
This is not identical. If user = 0, for example, it will not run, despite 0 being not None
@jasonmnayerji1783
@jasonmnayerji1783 Жыл бұрын
@@jack_papel I see what your getting at. However in this scenario considering we are dealing with strings, the only way the conditional evaluates to false is if the string is empty in which case we wouldn't want to execute the condition.
@Indently
@Indently Жыл бұрын
If you're only dealing with strings sure, but user can be anything, even it's own object.
@prabhatmishra8422
@prabhatmishra8422 Жыл бұрын
Me who is using: if user != None: print(user) 🤣🤣🤣🤣🤣🤣
@Indently
@Indently Жыл бұрын
PEP & the official Python documentation doesn't recommend what you did, mostly because it's the wrong use of "!=" and you should be using "is"
@prabhatmishra8422
@prabhatmishra8422 Жыл бұрын
@@Indently i don't change anything if it works 🤣🤣
@GlobeTrekMoto
@GlobeTrekMoto Жыл бұрын
if user != None: is better
@weirddev
@weirddev Жыл бұрын
if user:
@platinvm_
@platinvm_ Жыл бұрын
if user: >>>>
@MariusCiurea1
@MariusCiurea1 Жыл бұрын
Why not 'if user'? Cos it's gonna evaluate bool(user) which is True, so it prints out the user
@Indently
@Indently Жыл бұрын
Depends on what you are coding, 'if user' & 'if not None' are very different and do not perform the same check. That's up to the programmer though which functionality they want for which context.
@MariusCiurea1
@MariusCiurea1 Жыл бұрын
@@Indently I see, but this particular case, bool(None) is also False, so it can be used like this. if you make the user to point out to an empty string, the condition within the if will be True, printing out nothing. Indeed, if you just want to compare to None, I agree with you 😁
@Indently
@Indently Жыл бұрын
@@MariusCiurea1 When making this video I didn't expect to get so much attention regarding the variable itself, I was just trying to show the proper order for creating not None checks. I will pick a better example next time :)
@MariusCiurea1
@MariusCiurea1 Жыл бұрын
@@Indently I see. I said I agree with you. Even though I use python for years, I've learned new stuff from your videos. Keep it up!
@Indently
@Indently Жыл бұрын
@@MariusCiurea1 Happy to have you as a follower! Definitely don't be afraid to point anything out, I'm always happy to learn something new.
@sandiguha
@sandiguha Жыл бұрын
definitely the user isn't none, because they don't use computers in church.
@denizsincar29
@denizsincar29 Жыл бұрын
whaaat? i was writing if user!=None: wow. you made my day.
@LaughingOrange
@LaughingOrange Жыл бұрын
Python likes to swap cryptic symbols for English. It's part of what makes it great for new learners.
@denizsincar29
@denizsincar29 Жыл бұрын
@@LaughingOrange look to applescript and it'll be another level of english: set x to random number from 1 to 100 display dialog 'what is the number?' default answer '' set y to text returned of the result if x is equal to y then display dialog 'you guessed'
@QckSGaming
@QckSGaming Жыл бұрын
None of these are the most readable way. Guard clauses man, learn them and save all of our time
@thecringequeen31
@thecringequeen31 Жыл бұрын
User ifn’t none: print(Mario)
@ronaldodimaano8641
@ronaldodimaano8641 Жыл бұрын
`bool(None)` returns False so you can just do `if user:` or `if not user:`
@gigachad8810
@gigachad8810 Жыл бұрын
bool({}) also returns False, so does bool(""). you might have noticed that both of those are not None
@moon911x
@moon911x Жыл бұрын
it's funny 😃
@ewerybody
@ewerybody Жыл бұрын
lol everybody is like `if user:`!!! Nobody sees that `not user` will never be `None` so the last statement will ALWAYS print! 😂
@Indently
@Indently Жыл бұрын
A lot of people don't know the difference between "is" & "==", but I also gave a poor example for beginners, while what I mentioned in the video is correct, I can see why people would try to be smart about it and shorten it. Even if it's wrong and "NOT" the same thing 😅
@ewerybody
@ewerybody Жыл бұрын
@@Indently well, its about intend! If I WANT to catch user being other than None OR "" `if user:` is fine!
@AquaQuokka
@AquaQuokka Жыл бұрын
Never let them know your next move... if user is None: return else: ...
@not_vinkami
@not_vinkami Жыл бұрын
At least it isn't "not user == None"
@Indently
@Indently Жыл бұрын
It’s not even Halloween yet and you’re already scaring me !
@notzack6438
@notzack6438 Жыл бұрын
Damn python looks weird (I only learned c++ thus far)
@itsmaxim01
@itsmaxim01 Жыл бұрын
the correct way to do this is simply not coding in python
@memereview7415
@memereview7415 Жыл бұрын
Doesnt None work the same as False, and if so why not use "if not user:",
@Dalmen
@Dalmen Жыл бұрын
because it is not doing the same. you may use "if user:" But even this is not the same, because a empty string will as well give a True back.
@vinnythelegend
@vinnythelegend Жыл бұрын
Just do if user: Lol
@Dalmen
@Dalmen Жыл бұрын
bevor you laught at other, make sure you give a right answer. "if user:" don't do the same! A empty string will give here a FALSE as well back.
@vinnythelegend
@vinnythelegend Жыл бұрын
@@Dalmen You thought you were smart coming at me like that. You know what, I think I'm smart too. Therefore I actually went and tested it in Python myself. Both my example and the one in this video work the same, as I initially implied (both return False if the string is empty). Feel free to verify it yourself, if you even have Python installed... lol
@Yankzy
@Yankzy Жыл бұрын
Just say IF USER:
@Indently
@Indently Жыл бұрын
As long as you know the difference between if user is not None, and if user, you can do whatever you want.
@alext5497
@alext5497 Жыл бұрын
If (user) Yw
@gdmathguy
@gdmathguy Жыл бұрын
Wahoo user
@rootya.
@rootya. Жыл бұрын
import this
@zdwlees
@zdwlees Жыл бұрын
If user != None: :)
@ginomcfino4639
@ginomcfino4639 Жыл бұрын
The language is designed to handle the third way learn how to read code
@therodentous
@therodentous Жыл бұрын
its abolutely useless the most readeble and right is: if user:
@Indently
@Indently Жыл бұрын
It really depends, I think not knowing the difference between "user is not None" & "if user" is a really amateur mistake, considering they do 2 completely different things.
@pigeoni2
@pigeoni2 Жыл бұрын
"is not" haha ever heard of "!="
@Indently
@Indently Жыл бұрын
You don't use != with None (PEP)
@onlymeok
@onlymeok Жыл бұрын
!= is an inequality test, "is not" tests for object identity which is faster.
@ewerybody
@ewerybody Жыл бұрын
@@onlymeok `is` tests for identity! `not` negates to either True or False ☝️🤓
@mm552
@mm552 Жыл бұрын
What about: "user != None:" Also seems pretty readable. (And it's shorter)
@th2th210
@th2th210 Жыл бұрын
Most useless advice ever, just use if user ????????????
@jiaxiangchen3500
@jiaxiangchen3500 Жыл бұрын
haha,I wii write to def you_name(user): return 'your name'+user
@pandasoli6581
@pandasoli6581 Жыл бұрын
if user: print(user)
@johannesweber9410
@johannesweber9410 Жыл бұрын
if user:
@callmelord
@callmelord Жыл бұрын
if user:
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 253 М.
Python Sockets Simply Explained
39:33
NeuralNine
Рет қаралды 157 М.
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 13 МЛН
Beautiful gymnastics 😍☺️
00:15
Lexa_Merin
Рет қаралды 15 МЛН
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 9 МЛН
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 39 МЛН
10 Neat Ways To Use Underscore In Python
18:17
Indently
Рет қаралды 20 М.
the truth about ChatGPT generated code
10:35
Low Level Learning
Рет қаралды 219 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,6 МЛН
How To Use Modulo Operations In Python
4:25
Taylor's Software
Рет қаралды 141
LEVELS (Basic Version)
26:59
hoe_math
Рет қаралды 2,4 МЛН
Memoization: The TRUE Way To Optimize Your Code In Python
7:32
Why I Don't Use Else When Programming
10:18
Web Dev Simplified
Рет қаралды 1,2 МЛН
Cleaner Code: 3 Ways You Can Write Cleaner Code
7:41
Coding with Lewis
Рет қаралды 79 М.
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 13 МЛН