Python Tutorial: Clarifying the Issues with Mutable Default Arguments

preview_player
Показать описание
In this Python Programming Tutorial, we will be clarifying the issues with mutable default arguments. We discussed this in my last video titled "5 Common Python Mistakes and How to Fix Them", but I received many comments from people who were still confused. So we will be doing a deeper dive to explain exactly what is going on here. Let's get started...

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

I wanted to clarify this a bit since I got a lot of questions about this in my last video. In the last video, I was trying to keep the length of the video a bit shorter so I sacrificed a bit on the detail, but in hindsight, I probably should have taken the time to explain it a bit better. I hope this is a clear explanation for anyone who was still confused on this topic after the last video! Hope everyone is having a great week!

coreyms
Автор

The time demonstration near the end really helped me understand.
The default values are defined only once (during the function definition), but evaluated multiple times (during each function call).
However, if the default value is a mutable data-type (e.g. a list), it can be mutated/changed 'in-place' (i.e. without re-defining it).
Thank you!

zzzanon
Автор

Thanks Corey.. this really helped!! It cleared all my confusion. The datetime example that you gave is really awesome!

sandeepsuryaprasad
Автор

Thanks for this super helpful video! I've already bumped into the observation that we should never use mutable arguments as values to keyword arguments in python and instead set it to None and check before actually setting it, but I thought "mutable" was referring to arguments (in general) that the function or class would changes or alter itself, little did I now that "mutable" = python's mutable types (list, dict and set). Now that I gave it a second thought I couldn't make sense of how it really worked and it was driving me crazy, your video helped me a lot!

This kind of behavior remembers me a bit of C programming language with functions dealing with arrays, as all kinds of variables (but pointers) were passed by value and thus the function received but a copy of that variable and therefore any modifications it performed weren't reflect on the real variable outside of the function, meanwhile, arrays are passed by reference (reference of the memory address where the real array var is), so it's like the function is receiving the real array variable and the modifications performed with reflect on this same array outside of the function.

The relation I could make is that in Python, the mutable type variables used as keyword arguments are passed by reference, and the function manipulates the variable itself, thus for the next function call, the function is in fact will be taking that same default variable, but as it is right now with all the changes. Meanwhile, while with non-mutable types it's like the variable is being passed by value, just a copy of the real variable to act as a function's local variable which will exist only for the while the function is on stack, so on the next function call, the function will be getting the same default value unchanged.

gabriel-quintiliano
Автор

Best programming language being taught by the best teacher.
The main principle of python is "First class everything" which means everything is an object. Even a function is an object.
Function evaluates and stores all its defaults in a tuple and sets its attribute named as __default__.

Random fun fact: A class is an object of type object class in python. Peace.

ashfaqrahman
Автор

Thank Corey, very helpful. Not directly related, but if you could do a video "Shallow vs Deep Copying of Python Objects", that would be very appreciated.

archstampton
Автор

OMG i finally get this now, i spent about a week trying to figure out, why this piece of code was written in this manner, the "None" part tripped me off a bit, now its so much clearer. Great work!

alex
Автор

This was quite helpful. I learned this the painful way. Also happens with dicts, default args for classes. I knew that it existed, but didn't really understand why until now. I felt like I was doing something 'unprofessional' by setting the default arg to None. Seeing you do it made me feel comfortable. Thanks for the vid.

treelight
Автор

Some of the best videos. Makes me want to read the documentation and learn all the cool tricks

poke_champ
Автор

This level of explanation is really appreciated. Thanks for this!!!!

Michelle-ekdo
Автор

This is the best explanation I ve seen so far. Thank you soooo much!!

adsfwef
Автор

Thanks! Awesome explanations and example. Love the video

alanhwu
Автор

Thanks for clarifying, nice follow up.

greob
Автор

the datetime example at the end is really good!

init
Автор

Amazing Video, you are the best python teacher.

josephralph
Автор

As my understanding the pointer of default value is initialized once and won't be changed, then it depends on the pointed value is mutable or not ( a list is mutable while a datetime value is immutable) .

YMcarpediem
Автор

Thank you so much! This really helped!

niwanthakaweragoda
Автор

Hi Corey, thank you for all the great work. May I ask if this has anything to do with CLOSURES?

phamvantho
Автор

WOW. I never notice this kind of the issue. Somehow I think it is like a class that is initialized with default arguments. Do you have any insights about why Python behave as such?

tonyli
Автор

Dear Corey, thanks for this clear explanation. If I get it correctly, anything immutable should do and not just None. Then may I use an empty string ' ' instead of None? Is there any rationale to choose the None over other immutable variables?

dawei