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

Programming Camera-Relative Movement Controls

Why Everyone Has Productivity Software Wrong, and The Metaverse Has It Right

CRUD Operation using the SQLite module in Python

Classes in C#

3 Quick Ways to secure a new Linux server

Migrating AWS Lambda to Pulumi Project From Serverless

Tackling Big Data problems with Concept of Distributed Storage and Hadoop.

DEV Basics: How to set up your computer for Salesforce Development

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

Why You Should Use Python Data Classes

Functions vs Methods

Find duplicates using Python

CSV to JSON converter in 10 lines of code