Skip to main content
Mention why to use `is`, to address comments.
Source Link
wjandrea
  • 34k
  • 10
  • 69
  • 105
def my_func(working_list=None): if working_list is None: working_list = [] # alternative: # working_list = [] if working_list is None else working_list working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for ittest for it in the body of the function.

Aside

x is None is the comparison recommended by PEP 8:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

Also, beware of writing if x when you really mean if x is not None [...]

See also What is the difference between "is None" and "== None"

def my_func(working_list=None): if working_list is None: working_list = [] # alternative: # working_list = [] if working_list is None else working_list working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

def my_func(working_list=None): if working_list is None: working_list = [] # alternative: # working_list = [] if working_list is None else working_list working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

Aside

x is None is the comparison recommended by PEP 8:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

Also, beware of writing if x when you really mean if x is not None [...]

See also What is the difference between "is None" and "== None"

added 83 characters in body
Source Link
Nico Schlömer
  • 59.7k
  • 35
  • 216
  • 291
def my_func(working_list=None): if working_list is None: working_list = [] working_list.append("a") print(working_list) 
def my_func(working_list=None): if working_list is None: working_list = [] # alternative: # working_list = [] if working_list is None else working_list working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

def my_func(working_list=None): if working_list is None: working_list = [] working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

def my_func(working_list=None): if working_list is None: working_list = [] # alternative: # working_list = [] if working_list is None else working_list working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

change to python 3 and add explanatory note and link to the documentation
Source Link
def myFuncmy_func(working_list=None): if working_list is None: working_list = []   working_list.append("a") print (working_list) 

is how I do itThe docs say you should use None as the default and explicitly test for it in the body of the function.

def myFunc(working_list=None): if working_list is None: working_list = [] working_list.append("a") print working_list 

is how I do it.

def my_func(working_list=None): if working_list is None: working_list = []   working_list.append("a") print(working_list) 

The docs say you should use None as the default and explicitly test for it in the body of the function.

modified according PEP8 (http://www.python.org/dev/peps/pep-0008/) (comment edited Mar 11, 2019 at 0:42)
Source Link
jfs
  • 417.1k
  • 210
  • 1k
  • 1.7k
Loading
Source Link
HenryR
  • 8.7k
  • 7
  • 37
  • 39
Loading