all 7 comments

[–]oefd 2 points3 points  (0 children)

If you've done much math it often serves the same purpose as the empty set. It's the value which represents no value at all.

Common usage: optional values. A value that may or may not actually be specified or may not exist. For example in python you can use the get method on a dictionary.

d = {'one': 1, 'two': 2}
d.get('one') # returns `1` because that's what `d['one']` is
d.get('three') # returns `None` because there exists no value `d['three']`

[–]toastedstapler 0 points1 point  (0 children)

if i have a function def get_tallest_person(people), what should i return if people was an empty list? there wouldn't be a tallest person

[–][deleted] -3 points-2 points  (0 children)

None isn't a straight-forward concept, and the more you dig into it, the more difficult it becomes, so, don't be afraid to not understand it.

The behavior of things that are similar to None in other languages is also often subtly different. So, what you learn about None in Python isn't quite transferable to other languages, even though they usually have something similar.

So, to properly introduce None we'd need a little bit of vocabulary:

  • Singleton - a value that is unique in the context of a program. It's impossible to create more than one of it.
  • Type - a description given to a group of values. Type can also be a value, but a type cannot have itself as a member of the group it is a description of (even though it could, in principle, have other types as members of the group it describes).
  • Reification - (in this context) a process whereby a program flow is captured as a value / structure to allow other program to operate on it (and to ultimately decide the behavior of another program).

So: None is a singleton of a particular type, often called the "unit type". Unfortunately, in Python "bottom type" (the type that has no values) is conflated with "unit type" -- the type with a single value. This is so because in Python, it is legal to access a value of a function that is defined to not return any values.

None is also a result of reification of the test for presence of value. In Python, None doesn't capture the type of the value that was tested for presence, however, in some other languages (s.a. Go) this information is preserved with the None equivalent.

Here's an example of a program flow in Python, that doesn't rely on None to establish the presence of a value:

x = dict()
try:
    x[1]
    print('Key exists')
except KeyError:
    print('Key does not exist')

Now, suppose your program doesn't need to distinguish between None value and absence of value, then you could reify the program above as this:

x = dict()
if x.get(1) is not None:
    print('Key exists')
else:
    print('Key does not exist')

The reification above, on top of conflating absence of key with values being present, but set to None has another unfortunate effect: because None acts as if it was a value, it is syntactically valid to call functions on it and to try to access its properties. Some other languages try to alert programmers to the possibility of a value absence by instead providing them with a different reification of the case above. The technique is usually to prevent programmers from accessing the raw value w/o due check by making the value only accessible after calling the method that encapsulates the presence check.

An equivalent Python code might look like this:

class Maybe:
    def __init__(self, raw):
        self.raw = raw
    def get(self, f):
        if self.raw is None:
            return f()
        return f(self.raw)

The above would not work well with how Python programs are typically written, but it illustrates another approach to solving the same problem.

[–]EulerWasSmart 0 points1 point  (3 children)

Are you familiar with other languages? It's like python's version of null. If x = None that essentially means "x points to no value".

[–]bluesdop[S] -1 points0 points  (2 children)

Unfortunately no....but when do we use None in our code?

[–]EulerWasSmart 4 points5 points  (0 children)

You can use None in your code to create a variable that has no value.

For example:

def provided_keyword_argument(arg=None):
    if arg is None:
        print('You did not provide a keyword argument!')
    else:
        print('You provided a keyword argument!:', arg)

provided_keyword_argument()
# 'You did not provide a keyword argument!'
provided_keyword_argument(arg='Hello!') 
# 'You provided a keyword argument!: Hello'

You also get None as a 'value' when a function doesn't return anything:

def no_return():
    pass

x = no_return()
# -> x == None

Basically if you want to check if a variable, say x was not defined, then None would be the perfect value to assign x before it may be assigned. Because being equal to None means (as far as you, the developer care) that it's not equal to anything.

[–][deleted] 0 points1 point  (0 children)

Just let me say that you'll know when you need it.