Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.python
Re: Behavior of default parameter in a function
Gary Herron
3/11/2010 4:48:00 PM
This is addressed in the FAQ.
http://www.python.org/doc/faq/general/#why-are-default-values-shared-betwe...
jitendra gupta wrote:
>
> def foo(x = [0]):
> x[0] = x[0] + 1
> return x[0]
>
> def soo(x = None):
> if x is None:
> x = [0]
> x[0] = x[0] + 1
> return x[0]
>
> >>> foo()
> 1
> >>>foo() #See the behavior incremented by one
> 2
> >>>foo([1]) # but here based on given number
> 2
> >>>foo()
> 3
> >>>foo([1])
> 2
> >>>foo()
> 4
>
> >>>soo()
> 1
> >>>soo()
> 1
> >>>soo([1])
> 2
> >>>soo()
> 1
>
> Why foo() is incremented by 1 always when we are not passing any argument,
> but this is not happening in soo() case, In which scenario
> we will use these type of function.'
>
> Thanks
> Jitendra Kumar
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Behavior of default parameter in a function
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password