Комментарии:
Subscribed
ОтветитьMissed the chance to also show people the match-case in python, as that is the next logical thing that a dev needs to know.
Ответитьdef get_connection_status_message():
message = 'You are online!'
if not online:
message = 'You are offline'
if not internet:
message = 'No internet...'
if not paid:
message = 'User has not paid'
if not connection:
message = 'No connection...'
return message
print(get_connection_status_message())
What about creating helper functions for each "if"? E.g. check_connection(), check_payment(), etc that return a boolean. And then the go_online() function would look like:
def go_online():
if all(check_connection(), check_payment(), check_internet()):
print("You are online!")
Would that work?
dam not bad, nice vid
ОтветитьThis is also called inversion of your if statements. handle the sad paths first by inverting the statements, this forces the sad paths to the top
ОтветитьWhat is the theme setup used in this video?
ОтветитьTHANK YOU FOR THIS VIDEO!❤
ОтветитьNo, I think creating a dictionary is a better option.
ОтветитьI have a bit of a resistance for using more than one return into a function.
Ответитьnever knew this is called a guard clause. i do this all the time because white space is an anti pattern. makes code too hard to read
ОтветитьYandev be like: we don’t do that here
ОтветитьThis would be textbook case for "easier to ask forgiveness than permission" style in python?
try: connect()
except connection_error, payment_error, no_internet_error, user_not_online_exception: solve_the_problem
I m not a programmer, but using lists or dictionaries couldn't be even better, like this:
connection=False
paid=True
internet=False
online=True
l=[ ( connection, 'Your connection is on', 'Your connection is off'),
(paid, 'You have paid', 'You haven\'t paid'),
(internet, 'No connection', 'Internet has offline'),
(online, 'You are online', 'You aren\'t online')]
for k,z,x in l:
if k == True:
print(f'{z}')
else:
print(f'{x}')
Could you comment this code?
TIA
Nice! Is this a design pattern?
ОтветитьWhat we see here is not the problem of nesting ifs but the the biggest design flaw of Python: the lack of block closing symbol.
ОтветитьI mean, I'd use an if statement with &&/and to check every one of those. Unless I am in a situation where I can use return if certain conditions aren't met.
ОтветитьGreat tip. Much cleaner and readable.
ОтветитьExcellent video! After watching your video Python makes more sense.
Question?
Originally i am familiar with JavaScript for a little over 1 year. Just switched to Python because I will be focused on backend for the Meta course.
From your personal experience do you feel learning Python gave you a better understanding of programming if you came from another programming language? Trying to understand how Python could improve my JavaScript Skills, thank you.
Instead of having a return command inside every if statement, couldn’t you turn them into elif statements?
That way if one executes, the rest are skipped, no return command needed.
Then at the end have an “else” statement that confirms you are online if none of the other elif statements trigger .
this really getting compact with those conditions 🤟🏾 me too not a fan of nested condition ,What if dependency b/w condition any idea ,
ОтветитьHi thank you for another good tutorial and please take this as positive critism. I agree with you about the issue of reducing else statements, however I dont agree with your suggested solution, you have unnecessary return statements which could be avoided using `elif` statements instead.
Also I generally would have prefered one function call to print, here is my recommended solution for this:
def go_online():
message = 'You are online'
if not connection:
message = 'No connection...'
elif not paid:
message = 'User has not paid...'
elif not internet:
message = 'No Internet...'
print(message)
Code damage is minor problem... I think that jumping from statement to variable to statement to function in memory can damage ram
ОтветитьCan't you just do:
if not internet:
print("No internet")
elif not paid:
print("user has not paid")
else:
print("You are online")
it seems a lot more readable and it less lines of code
I'm not sure why Pythonistas avoid elif in cases like this. It's exactly why the switch/case statements are added to other languages. Someone else mentioned that old-school methods strongly favor a single return point (back to the days of hardware debuggers, I think - also mathematical proofs of function correctness), and tend to go there as well, but also readability for simple boolean checks like this. (Obviously doesn't really apply to more complex checks where the elif is working on different types of comparisons - not readable at all).
ОтветитьCan you do a quick follow-up video to check time efficiency?
ОтветитьWhat about using np.select() ?
ОтветитьI used to do a lot of nesting of if statement, but that was because I had read and had been taught it was bad to have more than one return statement per function.
ОтветитьWhy isnt my buddies `and` and `all` here!??
ОтветитьI would use something like assert but instead it would print and not error
ОтветитьI like to make those statements one liners
if not connection: return print("No connection")
You know your code's bad when it looks like HTML but isn't HTML
ОтветитьAhh yes, the yandev indentation.
Ответитьdef mult_checks(check_dict: dict[bool]) -> bool:
""" go through all items in dictionary
making sure they are True """
checks = all(list(check_dict.values()))
# Passed
if checks:
print('You are online...')
return True
# Failed
for k, v in check_dict.items():
if not v:
print(f'{k} Failed!')
return False
mult_checks({'online':True, 'paid': True, 'Internet': False})
I can't thank you enough, keep these amazing videos coming please!
Ответить