-5

When I was learning to code and trying to understand lines of codes given below, I am stuck :(

So I really wonder why when I assign name = None, first_name = None, last_name = None, the code still prints first_name and last_name(learned from a video about python dictionary), when I set something to None, why didn't Boolean gives me a False Error or even prints out nothing?

def packer(name=None, **kwargs):
  print(kwargs)


def unpacker(first_name=None, last_name=None):
  if first_name and last_name:
    print('Hi {} {}'.format(first_name, last_name))
  else:
    print('Hi no name!')

packer(name='Elgene', num=42, spanish_inquisition=None)
unpacker(**{'last_name': 'Ee', 'first_name': 'Elgene'})
Elgene
  • 3
  • 2
  • 1
    It's a null value – Alan Kavanagh May 19 '18 at 14:38
  • Welcome to StackOverflow. However, you are expected to do some research and work of your own before posting a question here. Did you do a web search on "python none" or something similar? – Rory Daulton May 19 '18 at 14:42
  • Because first and last names actually WERE assigned values. Parameters that come with = in a function are optional. If not passed in when the function is called, the function runs with whatever is after the = . In your case, unpacker is called with Ee as last name and Elgene as first name, so the function uses that instead of the default value None. That's why it prints out the name and not None. – thithien May 19 '18 at 15:54

1 Answers1

0

Like was said in the comments, None is a null value. It is useful when writing a code where "if" statements require a value to proceed....

import numpy

x=numpy.array([1.0,2.0,3.0])

if array_ is None: 
    pass
else: 
    x+=array_**2

where array_ is a numpy array that was assigned a value from arguments to the command line or gathered from a file (if the file exists). It is used in the code if present and is not used if not assigned a value (tends to be not crucial for the code to work).

Austin
  • 25,759
  • 4
  • 25
  • 48
bmatt
  • 1
  • 2