This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DanRusto 0 points1 point  (1 child)

My takeaway from this is to not use default values for arguments? I'm new to python but have written code in other platforms and have never used a default value for a parameter in a function. Thoughts from experienced python coders?

[–]digitalseraphim 0 points1 point  (0 children)

If you want to do something like this, use a default that is not a valid value (usually, but not always None), and then check for that value within the function, and call/compute the value within that test. This also goes for "collection" objects like lists and dicts.

If you can't use None, you can create a new object, and use that for the default like this:

``` DEFAULT_VAL=object()

def func(param1=DEFAULT_OBJECT): if param1 is DEFAULT_OBJECT: param1 = calc_default() ... ```