r/pythonhelp • • Apr 07 '21

Misunderstanding Functions: Keyword Arguments with Default Parameters

Hi. Working MIT 6.0001

Chapter 4.1.2 in the John Guttag Text

I'm having trouble understanding how to interpret the boolean here? In the first line of the body:

###########

if reverse:

print('Olga', 'Puchmajerova' )

###############

If reverse evaluates to True?

Or is it saying whatever you defined as the default parameter is a constant, and if the argument taken by the function is equivalent to that value, its true? So basically if you input false, then it evaluates to true? If you input True it's else?

I'm super confused here.

2 Upvotes

11 comments sorted by

1

u/Akshaykadav Apr 07 '21 edited Apr 07 '21

The if statement checks if a condition is true and when you say

If true : The condition is already true so it executes the if block

You can try doing print(5 > 1) And this will print True, the same happens when using a if statement


Now for your code, def fun(x , y, reverse = False):

For the default parameter reverse when you dont pass any value. It will use its default value which is false and

If false: The condition is false and will jump to the else block.

And when you pass True as a value to reverse it will forget its default value and will now use True as its value.

If true: The condition is true and will execute the if block.


You can check my video on default parameters if you want: https://youtu.be/n0RJ2zZwtcc (Not not that good with English but hope it helps.)

2

u/Remarkable-Avocado44 Apr 07 '21 edited Apr 07 '21

Thank you for this; that was among the clearest, calmest, simplest video descriptions i've seen. I just subscribed to your channel.

I believe i've understood your example.

A default function paramater is a placeholder; its exists as one of the parameters of the function, if that paramter is not supplied during the function call.

If the parameter is given during the function call, the default parameter is ignored/overidden.

1

u/Akshaykadav Apr 08 '21

Hey I know you said you just subscribed but I didn't see any subscriber change, if you don't mind can you check it again.

1

u/Remarkable-Avocado44 Apr 09 '21

Cool just fixed it

1

u/Akshaykadav Apr 09 '21

Can I ask how you did it, because for some reason I get a sub notification but the count doesn't increase and its happening a lot lately.

1

u/Remarkable-Avocado44 Apr 09 '21

hmmm I opened the youtube app on my phone, followed your link and hit subscribe... not sure what thats about!

1

u/Akshaykadav Apr 09 '21

Yeah, maybe youtube takes a few days to update the sub count.

1

u/Remarkable-Avocado44 Apr 24 '21

Did it go up?

1

u/Akshaykadav Apr 24 '21

Yup it takes around a day to actually update. Thanks for the follow up though 😊

1

u/Akshaykadav Apr 08 '21

Yup thats correct.

Thanks a lot for the sub and I am glad I was able to help you. I recently started started the channel so the audio for the some of the first videos is not that clear but I am improving.

If you want we can hangout at my discord server it currently empty: https://discord.gg/KqjpphFP

1

u/scoopulanang Apr 07 '21

Its saying if reverse evaluates to True. If you choose not to specify the value of a parameter that has a default value, its value will become the default value. I hope that makes a little bit of sense.