Re: [Tutor] functions and default argument
Prasad, Ramit wrote: Interesting thread and webpages. Insightful, but is this really used as a technique in daily practice? It feels a bit like a hack to me. Like the author of one of the websites said: rule #1 don't mess with this. I think the problem with rule #1 is that this can occur when you do *not* understand what is going on. The behavior can be non-intuitive for programmers coming from other (more statically-typed) languages and figuring out the programming error can be difficult depending on the complexity/design of the function or program. It actually has nothing to do with whether the language is statically typed or dynamically typed. It has everything to do with whether default arguments are early bound or late bound. That is, given the function definition: def func(arg=something): pass does the default argument `something` get created once (early binding, occurs one time only when the function is defined) or multiple times (late binding, each time the function is called)? In my experience, most people aren't even aware that there are two potential behaviours until they implicitly assume the one their language doesn't support. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
On 21/10/11 21:40, Albert-Jan Roskam wrote: Interesting thread and webpages. Insightful, but is this really used as a technique in daily practice? Yes, one example is where you use it for a counter to determine how often a function gets called: def reserveScarceResource(p1,p2,count = [0]): : count is mutable count[0] += 1 if count[0] > 100: raise UsedTooManyException # allocate resources Its not really a hack, it's an implementation feature and it's a fair impression of a closure in languages like Lisp, which makes it useful for certain classes of problem. It can also be useful in state machines where you want to carry through a state value across transitions but don't need to retain it after the state sequence completes. You don't need it often but its nice to have when you do! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
>Interesting thread and webpages. Insightful, but is this really used as a >technique in daily practice? It feels a bit like a hack to me. Like the author >of one of the websites said: rule #1 don't mess with this. I think the problem with rule #1 is that this can occur when you do *not* understand what is going on. The behavior can be non-intuitive for programmers coming from other (more statically-typed) languages and figuring out the programming error can be difficult depending on the complexity/design of the function or program. As for daily usage, well that depends on each person's needs; I do not need to write to files daily either, but I suspect most people would consider my Python knowledge lacking if I did not ;). I suspect you are right and it is not used on a daily basis by the majority of Python users/developers. In my opinion, knowledge > ignorance. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
Interesting thread and webpages. Insightful, but is this really used as a technique in daily practice? It feels a bit like a hack to me. Like the author of one of the websites said: rule #1 don't mess with this. Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ > >From: "Prasad, Ramit" >To: "tutor@python.org" >Sent: Friday, October 21, 2011 9:40 PM >Subject: Re: [Tutor] functions and default argument > >>The same thing occurs when you use a mutable object like a list or a >>dict. The default value is assigned once, and once only. But notice that >>you can modify the default value, say by appending to it: > >Not sure this will work exactly the same way in other IDEs, but in mine: > >>>> a = [] >>>> def foo(x=a): >... x.append(1) >... >>>> a.append( 2 ) >>>> foo() >>>> print a >[2, 1] >>>> help(foo) >Help on function foo in module __pieshell__: > >foo(x=[2, 1]) > >>>> foo() >>>> help(foo) >Help on function foo in module __pieshell__: > >foo(x=[2, 1, 1]) > >>>> a.append( 3 ) >>>> help(foo) >Help on function foo in module __pieshell__: > >foo(x=[2, 1, 1, 3]) >>>> b = [] >>>> foo(b) >>>> b >[1] > > >Notice how it is always bound to the list a, but can be "overridden". > >I know this has been discussed on this list or the main list before if you >take a look through the archives. >Sorry I can't remember what the thread would be like or when it was :( >http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument > > > >Ramit > > >Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology >712 Main Street | Houston, TX 77002 >work phone: 713 - 216 - 5423 > > > > >This email is confidential and subject to important disclaimers and >conditions including on offers for the purchase or sale of >securities, accuracy and completeness of information, viruses, >confidentiality, legal privilege, and legal entity disclaimers, >available at http://www.jpmorgan.com/pages/disclosures/email. >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor > > >___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
>The same thing occurs when you use a mutable object like a list or a >dict. The default value is assigned once, and once only. But notice that >you can modify the default value, say by appending to it: Not sure this will work exactly the same way in other IDEs, but in mine: >>> a = [] >>> def foo(x=a): ... x.append(1) ... >>> a.append( 2 ) >>> foo() >>> print a [2, 1] >>> help(foo) Help on function foo in module __pieshell__: foo(x=[2, 1]) >>> foo() >>> help(foo) Help on function foo in module __pieshell__: foo(x=[2, 1, 1]) >>> a.append( 3 ) >>> help(foo) Help on function foo in module __pieshell__: foo(x=[2, 1, 1, 3]) >>> b = [] >>> foo(b) >>> b [1] Notice how it is always bound to the list a, but can be "overridden". I know this has been discussed on this list or the main list before if you take a look through the archives. Sorry I can't remember what the thread would be like or when it was :( http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
Praveen Singh wrote: In function- "Default value is *evaluated only once*.This makes different when the default is a mutable object such as a list, dictionary or instance of most classes." I am not getting it properly-evaluated once?? different behaviour???-- please explain this. Look at an example: >>> import time >>> def test(t=time.asctime()): ... print t, "***", time.asctime() ... >>> time.sleep(30) # wait a little bit >>> test() Sat Oct 22 04:17:08 2011 *** Sat Oct 22 04:17:57 2011 >>> time.sleep(30) # wait a little bit longer >>> test() Sat Oct 22 04:17:08 2011 *** Sat Oct 22 04:18:46 2011 Notice that the first time printed, using the default value, is the same. The default value for t is assigned once, and not calculated again. Since t is a string, it is immutable and can never change. The same thing occurs when you use a mutable object like a list or a dict. The default value is assigned once, and once only. But notice that you can modify the default value, say by appending to it: >>> def test(x=[]): ... print x, id(x) ... x.append(1) ... >>> test() [] 3085600236L >>> test() [1] 3085600236L >>> test() [1, 1] 3085600236L It is the same default list every time, but the *contents* of the list are changing. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] functions and default argument
On 2011/10/21 03:00 PM, Praveen Singh wrote: In function- "Default value is *evaluated only once*.This makes different when the default is a mutable object such as a list, dictionary or instance of most classes." I am not getting it properly-evaluated once?? different behaviour???-- please explain this. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Mutable defaults for function/method arguments is a Python Gotcha, you can find a good read here [1]. It's a better read than how I would explain it. [1] http://www.ferg.org/projects/python_gotchas.html#contents_item_6 -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] functions and default argument
In function- "Default value is *evaluated only once*.This makes different when the default is a mutable object such as a list, dictionary or instance of most classes." I am not getting it properly-evaluated once?? different behaviour???-- please explain this. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor