1

Possible Duplicate:
Is there any way to know if the value of an argument is the default vs. user-specified?
python - returning a default value

It is standard practice in Python to use None as the default parameter value.

However, this idiomatic usage doesn't work well if client code may use None as a legal argument value. In this case, the function cannot tell whether the client code omitted the argument, or whether it explicitly passed None. This is not good, unless (by luck) the function's behavior should be the same in both cases.

What is a good solution?

Community
  • 1
  • 1
max
  • 49,282
  • 56
  • 208
  • 355
  • 1
    @JoshCaswell: My duplicate covers this better; the other talks about using `None` as the default without `None` being a legitimate value for the function. – Martijn Pieters Oct 23 '12 at 06:39

1 Answers1

5
_NOT_SET = object()

def some_function(some_arg=_NOT_SET):
    if some_arg is _NOT_SET:
        # ...

Any class instance has a unique identity that is can tell apart.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Is there any way to avoid namespace pollution? I guess not, since the default parameter value has will be evaluated when function definition is executed? – max Oct 23 '12 at 04:44
  • Correct. But if your functions are nicely broken up into modules and classes, you can use the tightest scope available to you - for instance, `_NOT_SET` can be a class member or a module member, without being global to your entire program. Using a name that starts with an underscore also helps - it's Python convention for something that external users shouldn't care about. – Amber Oct 23 '12 at 04:45
  • 1
    Getting a lot of mileage out of the semaphore trick tonight, Amber. (reference to question about the possible uses of a base object instance) LOL. I'm not upvoting this one though, since Q is duplicate. – mjv Oct 23 '12 at 04:46
  • I can only see an ugly way to do that, by saying `def some_function(some_arg = object())`, and then looking up default using `inspec`... I wouldn't want to do that :) – max Oct 23 '12 at 04:46
  • @mjv That's perfectly fine; I don't do this for the rep. The only reason I added this answer instead of also voting to close is that I don't think the linked duplicate actually addresses max's question very well. – Amber Oct 23 '12 at 04:47
  • @Amber, true, the duplicate post is a bit confusing because of it also deals with default values that are not objects. And ;-) about you -or I- not really rep driven (That's my excuse for not being so high on the leader board, but you on the other hand are doing quite well. Congrats). – mjv Oct 23 '12 at 04:58
  • In fact, the proposed duplicate question has an answer which your answer duplicates: http://stackoverflow.com/a/11251226/603977 – jscs Oct 23 '12 at 05:16
  • A more common name for this is simply `_marker` or `_default`. I've never seen `_NOT_SET`, and find it slightly confusing. – Lennart Regebro Oct 23 '12 at 07:17
  • @LennartRegebro I've never seen `_marker` nor `_default` before. I don't think there's really a true common standard for it; everyone uses whatever they're most comfortable with (or whatever is used in the project they first came across using it in). – Amber Oct 23 '12 at 07:33