[Tutor] How default arg of function works

2018-06-14 Thread Deepak Dixit
I am learning python and working with function.

Here is my test program :-

program.py

def test1(nums=[]):
return nums


def test2(nums=[]):
nums.append(len(nums));
return nums

print "Calling test1"
print '=' * 40
print 'test1()', test1()
print 'test1([1,2])', test1([1,2])
print 'test1()', test1()
print 'test1([1,1,1])', test1([1,1,1])
print 'test1()', test1()

print "Calling test2"
print '=' * 40

print 'test2()', test2()
print 'test2([1,2,3])', test2([1,2,3])
print 'test2([1,2])', test2([1,2])
print 'test2()', test2()
print 'test2()', test2()

--

# python program.py


Calling test1

test1() [ ]
test1([1,2]) [1, 2]
test1() [ ]
test1([1,1,1]) [1, 1, 1]
test1() [ ]
Calling test2

test2() [0]
test2([1,2,3]) [1, 2, 3, 3]
test2([1,2]) [1, 2, 2]
test2() [0, 1]
test2() [0, 1, 2]


I am assuming that in test1() we are not doing any modification in the
passed list and because of that its working as per my expectation.
But when I am calling test2() with params and without params then both are
using different references.  Why ? Can you please help me to understand
this.

-- 
With Regards,
Deepak Kumar Dixit
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How default arg of function works

2018-06-14 Thread Deepak Dixit
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.
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. Any suggestions from your
side will be really helpful for me.

On Thu, Jun 14, 2018, 1:00 PM Alan Gauld via Tutor  wrote:

> On 14/06/18 08:04, Deepak Dixit wrote:
>
> > def test2(nums=[]):
> > nums.append(len(nums));
> > return nums
> >
> > print 'test2()', test2()
> > print 'test2([1,2,3])', test2([1,2,3])
> > print 'test2([1,2])', test2([1,2])
> > print 'test2()', test2()
> > print 'test2()', test2()
> >
> > Calling test2
> > 
> > test2() [0]
> > test2([1,2,3]) [1, 2, 3, 3]
> > test2([1,2]) [1, 2, 2]
> > test2() [0, 1]
> > test2() [0, 1, 2]
> >
> > I am assuming that in test1() we are not doing any modification in the
> > passed list and because of that its working as per my expectation.
>
> Correct. You are still dealing with 2 separate objects(see below)
> but you don't modify anything so it looks like it works as you
> expected.
>
> > But when I am calling test2() with params and without params then both
> are
> > using different references.  Why ? Can you please help me to understand
> > this.
>
> When you create a default value for a function parameter Python
> creates an actual object and uses that object for every invocation
> of the function that uses the default. If the object is mutable
> (a list or class instance, say) then any modifications to that
> object will stick and be present on subsequent calls.
>
> Thus in your case the first call of the function the list is
> empty and has len() 0, the second call it still has the zero
> stored so len() is now 1, and so on.
>
> But when you call the function without using the default
> Python does not involve the default object at all, it uses
> whichever object you provide.
>
> The same applies to your tst1 function. Each time you use
> the default call the same empty list object is being printed.
> But because you don't modify it it always appears as
> an empty list.
>
> --
> 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
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How default arg of function works

2018-06-14 Thread Deepak Dixit
Thanks a lot for this information.

On Thu, Jun 14, 2018, 4:28 PM Alan Gauld via Tutor  wrote:

> 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
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help please

2018-10-10 Thread Deepak Dixit
On Wed, Oct 10, 2018, 12:37 PM Michael Schmitt 
wrote:

> To whom it may concern:
>
>
> I am trying to teach myself Python and ran into a problem. This is my code
>
>
> # name of rivers and country
>
> rivers = {'nile' : 'egypt', 'ohio' : 'US', 'rhine' : 'germany' }
>
> # prints river name
> for rivers in rivers.keys():
> print (rivers)
>
> #prints country
> for rivers in rivers.values():
> print (rivers)
>
> # prints statement " The (river) is in the country of (country)
>

  Why are you using "for rivers in rivers".
  Replace this for loop with :-

  for river in rivers:
 print ("The " + river + "is in the country of " + rivers.get(river))

for rivers in rivers:
> print ("The " + rivers.keys() + "is in the country of " +
> rivers.vaules())
>
> I am getting the following error
>  for rivers in rivers.values():
> AttributeError: 'str' object has no attribute 'values'
>
> Thanks for the help.
>
> Sincerely,
>
> Michael S. Schmitt
>
>
> [
> https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif
> ]<
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
>   Virus-free. www.avast.com<
> https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link
> >
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor