[Tutor] Python vs. MATLAB

Chris Fuller cfuller084 at thinkingplanet.net
Mon Dec 6 19:55:01 CET 2010

It also makes it hard to modify some data structure you are working with.  I 
was experimenting with some object-oriented programming in Matlab, and you 
actually have to explicitly inherit from "handle" if you have any methods that 
modify internal state.  That was a surprise!

If you've got a data structure built out of arrays or cell arrays and you want 
to not make copies everytime you pass it into or out of a function, you pretty 
much have to wrap it in one of these classes.  Another option is to use 
closures, with function handles, but that's pretty clunky also.


On Monday 06 December 2010, Wayne Werner wrote:
> On Mon, Dec 6, 2010 at 11:09 AM, Joel Schwartz <joel at joelschwartz.com>wrote:
> > Chris,
> > 
> > Can you say more about number (7) in your list? What does "pass by value"
> > mean and what are the alternatives?
> Pass by value is exactly what it sounds like - you pass the value (a copy
> of everything in the memory). This is bad when you're passing a 10,000
> item list to a function - because you now have *two* 10,000 item lists.
> It's even worse when you have many times that amount of data.
> Python, OTOH passes by reference - instead of copying the list, a pointer
> to the list is passed, so when you see something like this:
> def do_something(a_list):
>     a_list[2] = 4
> mylist = [1,2,3,4]
> do_something(mylist)
> now mylist is:
> [1,2,4,4].
> This is much more efficient (although it tends to bite novice
> programmers!).
> HTH,
> Wayne

More information about the Tutor mailing list