Python Concepts: Part None2014-09-20
Let’s learn about nothing. More particularly, let’s learnt about python’s concept of representing nothing,
None. This is the most nothingest concept of nothing as well - for example, zero is nothing, but it’s also a number, so it still has some information. Other names for nothing from other programming languages include
I’m writing this post in IPython. If you want to follow along at home, get your IPython console started by typing
ipython at the command prompt. Didn’t work? Install it. Bam.
Now, let’s start. Let’s see what happens if ask for nothing:
No output - makes sense. Let’s try outputting it to screen though:
Aha, we get some output.
string output of
'None'. Let’s check that
Now, since we didn’t use print, the output comes from the IPython prompt, with an “Out” marker to indicate that. IPython takes the expression you typed in, evaluates it, and displays the result as a prompt - unless the result is
None, when it displays nothing! And you guessed it, input 1 evaluated to
None, and so did input 2 (
So if we print again, there is no output prompt, since the print statement evaluates to nothing. Let’s try
Anyway, the string
"None" is something, and we’re here to investigate nothing. Let’s use an IPython feature to find out more about it:
None has a type - the
NoneType. In fact, every object in python has a type, which is like a
class in other languages, except it extends to builtin things as well.
The String Form there - that’s what you get when you pass
str as we’ve seen.
Namespace is ‘Python builtin’ - which means, surprise surprise, it’s built-in to Python.
There’s no docstring - this is a way of adding documentation to objects in Python for users to access. Let’s check for a docstring on another object we know about:
str’s docstring is much more instructive - and it tells us how to do what we did on line 3!
But its type is type? This means we can make fresh
str objects by calling the type:
And tada, a blank
Can we do that with the
NoneType I wonder?
Uh-oh. Looks like we don’t actually have
NoneType available in our current namespace - just
None. But luckily, we can ask for the type of
None, store it in a variable, and call that:
Dang. We can have a handle on the
NoneType type, but we can’t make any more of it. This is actually a special rule for the
NoneType… Does it mean there is only ever one
Well, they’re equal, that’s a start. But can we check that
b are exactly the same thing?
Yes indeed they are. So there is only ever one
None, and we can never make any more of it. That’s useful to know - write it down in your copy book now.
Let’s have a break with a quote on nothing:
Nothing proceeds from nothingness, as also nothing passes away into non-existence.
– Marcus Aurelius, Meditations, IV, 4
Let’s continue; I wonder if
None can be checked against:
Oh, nothing happened. Let’s see what happens if we go the other way:
None is never
True, and always
False? Let’s just check that by turning it into a
That explains why our
if statement worked that way -
None gets turned into
False when we test it as a
Let’s try doing some more day-to-day things with
So, you can’t really do anything. Makes sense that you do (nearly) nothing with nothing.
Let’s check its
directory of methods though - maybe it has something it hasn’t told us yet.
Wow, that’s a lot of things! How many things?
Wow, there are actually 15 things that
None can do!?
Let’s try from the top…
NoneType again? I thought we already got that guy with
type! Is that the same
Okay, that’s boring - just another way to get ahold of that
__class__ it looks like.
Let’s check the next one -
Okay, it’s a method-wrapper. Guess that means it’s like a method. Let’s try calling it.
Okay, you want an argument? I’ll give you an argument!
Oh, you want an argument that’s the name of an attribute? Why didn’t you say? We already know the name of an attribute that we don’t need -
__class__ is something we can get by calling
type - so let’s try saving some space by getting rid of that:
Well that’s annoying, but I guess it’s okay for python to have some protection over such built-in features. For all we know,
type might need the
__class__ attribute to be there to work, right?
I give up, let’s try the next one.
Oh great, nothing. This is actually the attribute that stores an object’s docstring though - and as we saw earlier,
None has no docstring.
Let’s check by getting the
str docstring in the same way:
Yup, that’s the string, although the new-line characters have been displayed as
Okay, let’s pass in an empty str.
Let’s try giving it the name of an attribute…
Wow, we just discovered that the ‘dot’ operator really just calls the
__getattribute__ method of the object! So you can get any attribute that way? Let’s try getting them all!
Wow, that saved some work, checking what they are and where they are from! Most seem to be part of the
NoneType object, but some are from
Apparently so! What else is an
Well, I heard python is object-oriented, but it seems oriented towards making everything an object.
So what does that mean? Does object have some stuff on it that you can do anywhere or something?
Holy smokes, that’s like the same list that
None has! Wait, is it exactly the same list?
Yes indeed! Aww wait, this means we learnt nothing interesting about
None - it only has all the attributes that every other
object has. So maybe it really doesn’t do anything!
But we’ve learnt some valuable lessons:
- There is only one
- The type of
- Most types let you create more objects of their type, but not
Nonedoes nothing interesting
- If you turn
string, it becomes
'None'. If you turn
boolean, it becomes
That’s all we have time for in this blog post! Next time:
False enter the picture in full focus.
© 2019 All rights reserved.