[Tutor] How default arg of function works

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jun 14 06:56:49 EDT 2018


On 14/06/18 08:40, Deepak Dixit wrote:
> You mean that for default args and passed args of mutable type, python uses
> different object and same reference will be used for further calling of the
> function.

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.

When you provide the argument yourself Python uses
whatever object you pass.

> Now one more thing I want to ask you that how can I get deep understanding
> of python like how list, dictionary works internally and other topics , is
> it only possible after more and more practice. 

- You can observe the behaviour as you have been doing.
- You can use the disassembly module (dis) to look at
  the object code generated by the compiler.
- And, the ultimate truth, you can read the C source code
  if you know C.

And of course you can ask questions here. We have several
contributers who understand Python internals quite well.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list