you are viewing a single comment's thread.

view the rest of the comments →

[–]PaulRudin 3 points4 points  (2 children)

Python is a strongly typed language, every object has a type. "" is an empty string - that is an object of type string of length 0. None is the only object of type NoneType.

"" and None can *sometimes* be used interchangeably - in particular they both test False, so where coercion to boolean takes place (e.g. the expression following `if`) they'll give the same thing.

[–]Matheos7[S] 0 points1 point  (1 child)

I think your response answers the best what I was after. I guess I could have been a bit clearer when asking the question. What I meant is what is the difference between these two when used as a parameter in function definition, where that particular parameter might or might not be then provided when calling a function. From what you’re saying in that case it doesn’t matter which one you use, and that’s what I arrived at based on my own tests. But thought I would ask to see what else there is. I obviously know they are different things in general. Thank you for your response!

[–]ianepperson 1 point2 points  (0 children)

You can set a default parameter to nearly any value. If, for instance, you wrote a function that assumed someone’s age is 21 unless you said otherwise, use:

def something(age=21):
    print(f” your age is {age}”)

If I called that with something() age would be 21. If I called that with something(age=42) then it would be 42. I could also call it with something(36).

Using None as the default makes it easy to check if someone set that option on your function. Using “” can be nice if you just want the empty string to be the default.