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.
Related Contents:
- “Least Astonishment” and the Mutable Default Argument
- Python, default keyword arguments after variable length positional arguments
- Using an attribute of the current class instance as a default value for method’s parameter [duplicate]
- Argparse optional positional arguments?
- What is the difference between ‘/’ and ‘//’ when used for division?
- Filter dataframe rows if value in column is in a set list of values [duplicate]
- Sorting list based on values from another list
- SQLAlchemy IN clause
- How do I update a Python package?
- HTTP requests and JSON parsing in Python
- sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
- Writing a Python list of lists to a csv file
- Calculate difference in keys contained in two Python dictionaries
- What’s a good rate limiting algorithm?
- Python os.path.join() on a list
- pandas – add new column to dataframe from dictionary [duplicate]
- Python: Mocking a context manager
- Get a list of all the encodings Python can encode to
- pip freeze creates some weird path instead of the package version
- How to execute Python scripts in Windows?
- How do I compare two strings in python if order does not matter?
- model not showing up in django admin
- A get() like method for checking for Python attributes
- Working with Anaconda in Visual Studio Code
- How to check if an object has an attribute?
- Turn string into operator
- No module named ‘openpyxl’ – Python 3.4 – Ubuntu
- Are locks unnecessary in multi-threaded Python code because of the GIL?
- Python – How to extract the last x elements from a list [duplicate]
- Python PEP8: Blank lines convention
- OperationalError, no such column. Django
- Why can’t you add attributes to object in python? [duplicate]
- How to extract parameters from a list and pass them to a function call [duplicate]
- Django ORM – objects.filter() vs. objects.all().filter() – which one is preferred?
- Python: Why does (“hello” is “hello”) evaluate as True? [duplicate]
- Python and SQLite: insert into table
- How to override the pip command to Python3.x instead of Python2.7?
- What is the best way to create a string array in python?
- Delete every non utf-8 symbols from string
- Using Basic HTTP access authentication in Django testing framework
- Why Anaconda does not recognize conda command?
- Python here document without newlines at top and bottom
- Solution for SpecificationError: nested renamer is not supported while agg() along with groupby()
- overriding bool() for custom class [duplicate]
- Ending an infinite while loop
- What is the Python way to walk a directory tree?
- Matplotlib adjust figure margin
- Does Python support MySQL prepared statements?
- Copy upper triangle to lower triangle in a python matrix
- multiprocessing.Pool – PicklingError: Can’t pickle : attribute lookup thread.lock failed