Uncategorized

None in Python – Python Morsels



Let’s talk about Python’s None value.

Python’s None value

Python has a special object that’s typically used for representing emptiness.
It’s called None.

If we look at None from the Python REPL, we’ll see nothing at all:

Though if we print it, we’ll see None:

>>> name = None
>>> name
>>> print(name)
None

When checking for None values, you’ll usually see Python’s is operator used (for identity) instead of the equality operator (==):

>>> name is None
True
>>> name == None
True

Why is that?

Well, None has its own special type, the NoneType, and it’s the only object of that type:

>>> type(None)
<class 'NoneType'>

In fact, if we got a reference to that NoneType class, and then we called that class to make a new instance of it, we’ll actually get back the same exact instance, always, every time we call it:

>>> NoneType = type(None)
>>> NoneType() is None
True

The NoneType class is a singleton class.
So comparing to None with is works because there’s only one None value.
No object should compare as equal to None unless it is None.

None is falsey

We often rely on the falsiness of None in Python.
So instead of checking whether an object is None, we could check whether that object is falsey.

This works particularly well if all of the objects we’re working with are either truthy or None.

For example, the search function in Python’s re module always returns either None or a match object, and match objects are always truthy.
So instead of asking if match is not None, we can just say if not match:

>>> import re
>>> email = "[email protected]"
>>> match = re.search(r"^\S+@\S+[.]\S+$", email)
>>> if not match:
...     print("That doesn't look like an email address")
...

This also works well if a falsey object should be interpreted the same way as None.
For example, if an empty string and None should both be handled the same way:

>>> name = ""
>>> if not name:
...     print("No name was given")
...
No name was given

In cases like this, checking for falsiness rather than checking for None is actually preferable.

None represents emptiness

Where does None come up?
Well, conventionally None is used for saying “there’s nothing here”.

The dictionary get method is a good example of where None appears.
The get method can look up a value for a key or return a default value when that key is missing:

>>> fruits = {"apple": 3, "banana": 5, "lime": 6}
>>> pears = fruits.get("pear", 0)
>>> pears
0

But if no default value is given, we’ll get None:

>>> plums = fruits.get("plums")
>>> print(plums)
None

None is also sometimes used as the default value for function arguments.
For example, the string split method will treat None the same way as if no argument had been given at all:

>>> hughes = "Does it dry up\nlike a raisin in the sun?\n"
>>> hughes.split(None)
['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']
>>> hughes.split()
['Does', 'it', 'dry', 'up', 'like', 'a', 'raisin', 'in', 'the', 'sun?']

The default function return value is None

Every function in Python returns something, even functions without any return statement.
If a function has no return value, it returns None.

So if you call a function and it always seems to return None, it’s probably because that function isn’t meant to return anything:

>>> def greet(name):
...     print("Hello", name)
...
>>> result = greet("world")
Hello world
>>> print(result)
None

See print versus return for more on Python’s default function return value.

None is like NULL in other programming languages

Python’s None value is similar to NULL or NIL in other programming languages.

None is the default return value of a function in Python, and it’s often used to represent something that’s missing or empty.

It’s common to use falsiness checks when looking for None values.
But you’ll sometimes see the is operator used to explicitly look for None.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *