3

What is the best practice?
1) have a function be able to take in None?
2) practice to not to send None to a func

is it just personal preference or are there any pros/cons to this?

I have a func

def parse_path(path):
    site = None

    site_pattern = re.compile('(fl|ny|wa|tx)')
    match = site_pattern.search(path)
    if match:
        site = match.group(0)

    return site

so obviously if i pass in None to parse_path, it will complain.
TypeError: expected string or buffer

so should I always be conscious of what to put in to a func or should a func be flexible so it deals with None?

5
  • 6
    Why is it the functions fault if something else passes in the wrong type? None is not special here. What if I passed in a list, or a float instead? Commented May 29, 2014 at 14:13
  • @MartijnPieters so there should be a type check within a func? Commented May 29, 2014 at 14:14
  • 3
    No, there shouldn't be. It's the responsibility of the caller to pass in the correct types. If None is not a supported type, then you should fix the code calling your function to not pass in None. Commented May 29, 2014 at 14:16
  • 1
    @ealeon If someone passes None or a non-string path this function will fail quickly without a check. Treating path=None as a special case would only be necessary if path was an optional parameter. Commented May 29, 2014 at 14:17
  • as @IanStevens said, ultimately what's important is to fail fast and explicitly. As long as your function does that, you should be ok. Commented May 29, 2014 at 14:25

1 Answer 1

2

I'm using None as default sometimes as in:

def dosomething(input1, input2=None):

    if not input2:
        input2 = compute_default_input2

    carry_on()
    ....

This can be useful in classes that can be used in multiple ways or where certain properties require computationally intense initialisation, so you don't want to do this unless it's requested -- adding None as default makes that possible. It's also a cheap way to allow users to override an object's properties:

def dosomething(self, property=None):

    if not property:
        property = self.property

This will use the self.* value by default but allow the user to override it. It's also something of a hack, so as most things in Python should probably used with care.

... other than that: I think None as an input should only be dealt with in a function if there's a reasonable use case where None is passed to that function, and that very much depends on the environment that it is operating in. If you can reasonably expect everyone to be aware they shouldn't call your function with None then the standard error message from Python should be enough to make clear what the problem is.

Sign up to request clarification or add additional context in comments.

2 Comments

a shorter way to write that is property = property or self.property
You might want to test for property is not None since not property evaluates to True for False or any empty list or string as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.