Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:18:47+00:00 2026-05-13T23:18:47+00:00

I am considering moving from Matlab to Python/numpy for data analysis and numerical simulations.

  • 0

I am considering moving from Matlab to Python/numpy for data analysis and numerical simulations. I have used Matlab (and SML-NJ) for years, and am very comfortable in the functional environment without side effects (barring I/O), but am a little reluctant about the side effects in Python. Can people share their favorite gotchas regarding side effects, and if possible, how they got around them? As an example, I was a bit surprised when I tried the following code in Python:

lofls = [[]] * 4    #an accident waiting to happen!
lofls[0].append(7)  #not what I was expecting...
print lofls         #gives [[7], [7], [7], [7]]
#instead, I should have done this (I think)
lofls = [[] for x in range(4)]
lofls[0].append(7)  #only appends to the first list
print lofls         #gives [[7], [], [], []]

thanks in advance

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:18:48+00:00Added an answer on May 13, 2026 at 11:18 pm

    Confusing references to the same (mutable) object with references to separate objects is indeed a “gotcha” (suffered by all non-functional languages, ones which have mutable objects and, of course, references). A frequently seen bug in beginners’ Python code is misusing a default value which is mutable, e.g.:

    def addone(item, alist=[]):
      alist.append(item)
      return alist
    

    This code may be correct if the purpose is to have addone keep its own state (and return the one growing list to successive callers), much as static data would work in C; it’s not correct if the coder is wrongly assuming that a new empty list will be made at each call.

    Raw beginners used to functional languages can also be confused by the command-query separation design decision in Python’s built-in containers: mutating methods that don’t have anything in particular to return (i.e., the vast majority of mutating methods) return nothing (specifically, they return None) — they’re doing all their work “in-place”. Bugs coming from misunderstanding this are easy to spot, e.g.

    alist = alist.append(item)
    

    is pretty much guaranteed to be a bug — it appends an item to the list referred to by name alist, but then rebinds name alist to None (the return value of the append call).

    While the first issue I mentioned is about an early-binding that may mislead people who think the binding is, instead, a late one, there are issues that go the other way, where some people’s expectations are for an early binding while the binding is, instead, late. For example (with a hypothetical GUI framework…):

    for i in range(10):
        Button(text="Button #%s" % i,
               click=lambda: say("I'm #%s!" % i))
    

    this will show ten buttons saying “Button #0”, “Button #1”, etc, but, when clicked, each and every one of them will say it’s #9 — because the i within the lambda is late bound (with a lexical closure). A fix is to take advantage of the fact that default values for argument are early-bound (as I pointed out about the first issue!-) and change the last line to

               click=lambda i=i: say("I'm #%s!" % i))
    

    Now lambda‘s i is an argument with a default value, not a free variable (looked up by lexical closure) any more, and so the code works as intended (there are other ways too, of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try (untested): $.getJSON("data.php", function(data){ $.each(data.justIn, function() { $.each(this, function(k, v)… May 15, 2026 at 7:47 am
  • Editorial Team
    Editorial Team added an answer Does anyone know if it's possible to send out custom… May 15, 2026 at 7:47 am
  • Editorial Team
    Editorial Team added an answer You can use the getsockname function. The getsockname() function retrieves… May 15, 2026 at 7:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.