[Tutor] Function definition with default argument
Richard Damon
Richard at Damon-Family.org
Sat Aug 22 15:34:41 EDT 2020
On 8/22/20 3:17 PM, Alan Gauld via Tutor wrote:
> On 22/08/2020 17:51, Manprit Singh wrote:
>
>> import pandas as pd
>> ser = pd.Series(["M", "F", "M", "F", "F", "M"])
>> function, and if i write a function and call it as given below ,
>> passing the series as a default argument in the function :
>>
>> def offcnt(x=ser):
>> return x.value_counts()
> You almost certainly do not want to do that.
> The reason is that the default argument is the value
> of ser when you define the function. If you change ser
> to something else and then call your function without
> arguments it will still use the old value of ser.
It actually doesn't bind to the original values, but the original
object. If ser is rebound by assignment, you won't see the change, but
if it is mutated to some other state you will.
--
Richard Damon
More information about the Tutor
mailing list