[Tutor] functions and default argument
Steven D'Aprano
steve at pearwood.info
Sat Oct 22 02:43:10 CEST 2011
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
More information about the Tutor
mailing list