Common mistakes we make in Python and why we should avoid that part 3

def mutable_test_function(first, second=[]):
second.extend(first)
return second
mutable_test_function([1,2,3], [1,2,3])
Out[63]: [1, 2, 3, 1, 2, 3]
In [64]: mutable_test_function([1,2,3])
Out[64]: [1, 2, 3]
In [65]: mutable_test_function([1,2,3])
Out[65]: [1, 2, 3, 1, 2, 3]
In [66]: mutable_test_function([1,2,3])
Out[66]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
def mutable_test_function(first, second=None): 
if second is None:
second = []
second.extend(first)
return second
In [74]: mutable_test_function([1,2,3])
Out[74]: [1, 2, 3]
In [75]: mutable_test_function([1,2,3])
Out[75]: [1, 2, 3]
In [76]: mutable_test_function([1,2,3])
Out[76]: [1, 2, 3]

--

--

--

Coding, Reading,Python

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a GitHub Issues Tracker with Django and Notion API

Programming Camera-Relative Movement Controls

Postgres Performance Scripts

Analyzing Stock Price Time Series with Fortran Arrays, Part 2

How does Git Work?

Introducing Pavlok iOS App 4.0

Finding the best mutual fund to invest your money — Part 1: Web scraping with Scrapy in Python

Understand rebasing & YMPL differences

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afsalms

Afsalms

Coding, Reading,Python

More from Medium

Python Programming — Function

Python isn’t bad. Programmers are.

File: A file is a collection of data stored in an enclosed unit( the data can be text, image etc.),

Best module for python developer.