Home > other >  Is there any use for having an actual value as None and with type 'NoneType'?
Is there any use for having an actual value as None and with type 'NoneType'?

Time:02-19

I'm really green to programming, so please don't feel offended if it's a really silly question :). I learnt that only None can be None. Then when exercising I came across this:

multiplying a string returns a string of course - but when print it, returns without the '

'9' * 5 ->      # '99999'
print('9' * 5)  #  99999

I wondered what type could it be, so I tried to find out - and it gave this result:

def printer(y):      # printer('9' * 5)
    y = print(y)     # 99999
    d = y
    print(d)         # None
    return type(y)   # NoneType

I'm using Python 3.8.8

CodePudding user response:

The print() function doesn't return any value, hence when you do

y = print(y)  

You get y = None. None is simply used to define a null value.

By the way, you can get the type of None by simply doing

print(type(None))

Output:

<class 'NoneType'>

CodePudding user response:

First misconception:

That the thing you see when you run '9' * 5 in the interpreter is a different type than the thing you see when you print('9' * 5) because one is wrapped in single-quotes, the other isn't.

You're just seeing two different representations of the same kind of object - a string. Just because it doesn't have the single-quotes around it when you print it doesn't mean it's suddenly a different type. When you print, you are sending characters to the STDOUT pipe to be displayed in a human-friendly way, so naturally the single-quotes, which are only meaningful in the context of source code, are not displayed.

Second misconception:

That you would be able to capture the "type" of this mysterious unquoted string by binding the result of print to a variable, and then checking its type.

print always returns None. It's only useful for its primary side-effect, which is printing stuff to STDOUT.

CodePudding user response:

Strings don't contain the ' or " at each end. That's just syntax to show Python where the string starts or stops. To include quote marks in a string, you'll need to put them in yourself:

# these all print the same thing:
print('I said "hello" to you')
print("I said \"hello\" to you")
print("""I said "hello" to you""")

CodePudding user response:

Perhaps this might help:

'9' * 5
  • In repl console the next line is --> '99999'
  • In a program nothing is displayed or stored
print('9' * 5)
  • In both cases, the next line displayed is --> 99999
y = '9' * 5
  • There is no "ouput" in either case but the variable y is set to the string "99999"
def foo(i):
    print(i*2)
    return "hi"

x = foo(1)
  • In both cases, the next line displayed is --> 2
  • In both cases, the result "returned" by calling the function is assigned to the variable x
  • x is assigned the string "hi"
def foo(i):
    print(i*2)

x = foo(1)
  • In both cases, the next line is --> 2
  • In both cases, the result "returned" by calling the function is assigned to the variable x
  • x is assigned None as that is implicitly returned here in the absence of an explicit return like the prior example
x = print(2)

## and by extension

def foo(i):
    return print(i*2)

x = foo(1)
  • In all cases, the next line displayed is --> 2
  • In all cases, the result "returned" by calling the function is assigned to the variable x
  • Remembering this has nothing to do with what is displayed...
  • In all cases, x is assigned None.
  • Related