Re: Python newbie



On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:

There are several questions:

1) Why is the array "a" unchanged after undergoing a transformation with
map?

Because `map()` creates a new list and doesn't change the elements in `a`.

2) Why is it illegal to pass a built-in function "print" to map?

Because ``print`` isn't a function but a keyword. This will be changed
in Python 3.0. On the other hand this use case is discouraged because
`map()` builds a new list with the return value of the given function.
So you would build a list full of `None`\s just for the side effect.

3) Why is the array "a" unchanged after undergoing an explicit
transformation with the "for" loop?

Because there is no explicit transformation. The loop binds elements to
the name `x` in your example and within the loop you rebind that name to
another value. Neither the name `x` nor the objects bound to it have any
idea that the object might be referenced in some container object.

4) Is there an equivalent to \$a (Perl "reference") which would allow me
to decide when a variable is used by value and when by reference?

No, variables in Python are always name to object bindings. Don't think
of variables as boxes with names on it where you put objects in, or
references to another box. Think of objects and sticky notes with names
on it.

How can I make sure that
for x in a: x=2*x

actually changes the elements of the array "a"?

Well, actually change the elements. ;-)

Either:

for index, item in enumerate(sequence):
sequence[index] = func(item)

Or:

sequence[:] = map(func, sequence)

But the need to do so doesn't seem to be that frequent. Usually one just
builds a new list instead of altering an existing one.

Ciao,
Marc 'BlackJack' Rintsch
.



Relevant Pages

  • Re: arrays of arrays question
    ... > in ONE array. ... storing a reference to @row in the @rows array. ... in @row when the loop exits. ... and does not go out of scope. ...
    (comp.lang.perl.misc)
  • Re: Add a Value to an ArrayList
    ... add the VALUE of u at the point in the for loop, ... don't create a new instance and assign its reference to "u" each time ... then you're going to wind up with an array that has the ... reference will result in getting the same data regardless of which entry in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How is this collection called?
    ... you get a possible reference loop when you add the set to itself. ... If you have an array a of references, and add a reference to the array a ... get in trouble - the data structure's graph would have loops, ...
    (comp.theory)
  • Indexing a control
    ... In VB6 I could form an array of labels and reference them by index number. ... I'd like to use a for loop ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Continuous Subforms
    ... there is not multiple instances of the controls. ... you can't really reference the controls like an array. ... the "general" program loop to process a reocrdset is: ...
    (microsoft.public.access.forms)