On 06/14/2018 05:01 AM, Deepak Dixit wrote: > Thanks a lot for this information. > > On Thu, Jun 14, 2018, 4:28 PM Alan Gauld via Tutor <tutor@python.org> wrote:
>> Yes, the default argument object is created when the >> function is defined (ie before it is even called the >> first time) and the same reference to that obje3ct is >> always used for the default argument. this turns out to be one of the "surprises" in Python that catches quite a few people. once you know how things work under the covers, it makes sense why it is so, but it's a little unusual on the surface. def (to define a function) is actually an executable statement that is run when encountered. The result is a function object, bound to the name you def'd. One of the long-time Python luminaries wrote about this ages ago, I just went and hunted up the link in case you're interested (not that lots of other people haven't written about it, but Fredrik Lundh's comments are usually worth a read, and he's got some advice for you if you're seeking to get less surprising behavior (that is, don't use an empty list as the default, instead use a placeholder like None that you can check for): http://www.effbot.org/zone/default-values.htm _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor