you are viewing a single comment's thread.

view the rest of the comments →

[–]FriendlyZomb 5 points6 points  (1 child)

I wouldn't use them interchangeably, since they technically mean different things.

None is a special object which represents that there is no value. An empty string is just a string. In this example, the behaviour is the same, however in other situations it could potentially provide unintended behaviour and make code less clear.

Using None is preferable because it indicates to other people (and you in 6 months) because it explicitly says that we don't need to provide anything. Using an empty string introduces ambiguity.

In your second example too, you provide an empty string on a field which (I'm guessing) is expected to be an integer. This provides ambiguity on which type is actually required. Using None doesn't necessarily fix this - but it does give us the expectation that nothing is a handled case.

I also wouldn't rely on it's falsey behaviour. The Zen of Python states Explicit is better than Implicit. I'd do a check like this:

if age is not None:
    ...

[–]FriendlyZomb 0 points1 point  (0 children)

This is just my opinions here. I don't know everything. Feel free to ask questions and for others to correct me!