By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,533 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,533 IT Pros & Developers. It's quick & easy.

Mapping None. Why?

P: n/a

Iam wondering why the peculiar behavior of map when the function in
given as None:

Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -list

Return a list of the results of applying the function to the items
of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length. If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).
It seems as the action whith none is the same as using a function of
lambda *x: x
As in the following example:
>>l1 = 'asdf'
l2 = 'qwertyuip'
l3 = range(3)
l1,l2,l3
('asdf', 'qwertyuip', [0, 1, 2])
>>map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)
True
>>>

On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

Thanks, Paddy.

Jun 27 '08 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Paddy schrieb:
Iam wondering why the peculiar behavior of map when the function in
given as None:

Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -list

Return a list of the results of applying the function to the items
of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length. If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).
It seems as the action whith none is the same as using a function of
lambda *x: x
As in the following example:
>>>l1 = 'asdf'
l2 = 'qwertyuip'
l3 = range(3)
l1,l2,l3
('asdf', 'qwertyuip', [0, 1, 2])
>>>map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)
True
On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?
Because it is undefined what should happen in case of no function given
at all - and because there is no identity function in python
pre-defined, it could be considered sensible to make None the quivalent
of that function.

And it only follows that *if* you imply a function even though there is
None given, that the passed tuple is returned.

I don't see anything on wikipedia that defines any other behavior.

Diez

Diez
Jun 27 '08 #2

P: n/a
On Thu, Jun 12, 2008 at 1:05 PM, Paddy <pa*******@googlemail.comwrote:
>
Iam wondering why the peculiar behavior of map when the function in
given as None:
Because that's the way it's always been! Seriously, I don't know. I
can tell you that it's going away in Python 3.0, though.

Ian
Jun 27 '08 #3

P: n/a
On Thu, Jun 12, 2008 at 1:32 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
Because it is undefined what should happen in case of no function given at
all - and because there is no identity function in python pre-defined, it
could be considered sensible to make None the quivalent of that function.
It makes more sense to raise an error when a non-function is passed
where a function is expected. If we're going to have a special
behaviour for None, why not have special behaviours for True, False,
and 42 as well? The proper solution to the lack of a built-in packing
(not identity) function is to define a packing function, not to
special-case an arbitrary value to *mean* the packing function in
certain situations.

Ian
Jun 27 '08 #4

P: n/a
Ian Kelly wrote:
On Thu, Jun 12, 2008 at 1:05 PM, Paddy <pa*******@googlemail.comwrote:
>Iam wondering why the peculiar behavior of map when the function in
given as None:

Because that's the way it's always been! Seriously, I don't know. I
can tell you that it's going away in Python 3.0, though.
There was a time before zip(). Basically, it's a really useful feature, and the
original implementor thought that map() was a reasonable place to put it; it's a
somewhat natural outgrowth of the map(func, list1, list2, ... listn) semantics.
Since then, after more thought was put into it, it was realized that a separate
builtin function is more appropriate for this common use, and thus zip() was
born and map(None, ...) was deprecated.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #5

P: n/a
Ian Kelly schrieb:
On Thu, Jun 12, 2008 at 1:32 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
>Because it is undefined what should happen in case of no function given at
all - and because there is no identity function in python pre-defined, it
could be considered sensible to make None the quivalent of that function.

It makes more sense to raise an error when a non-function is passed
where a function is expected. If we're going to have a special
behaviour for None, why not have special behaviours for True, False,
and 42 as well? The proper solution to the lack of a built-in packing
(not identity) function is to define a packing function, not to
special-case an arbitrary value to *mean* the packing function in
certain situations.
You are right with the packing-function, it's not identity of course.

However I don't see that this as an area that is really important
(especially since map is being replaced by list-comps most of the time).

And the OP's question was about map not being conforming to the
definition on wikipedia - which I don't think it's not. It is not
defined what map is to do with None (or NULL or nil or... ) as argument.

Diez
Jun 27 '08 #6

P: n/a

"Paddy" <pa*******@googlemail.comwrote in message
news:a8**********************************@27g2000h sf.googlegroups.com...
|
| Iam wondering why the peculiar behavior of map when the function in
| given as None:

The 'peculiar behavior' is the same as zip (except for padding short
iterators versus truncating long iterators. Map was added years before
zip. After that, map(None,...) was kept for back compatibility.

In 3.0, the doc for map is
"Return an iterator that applies function to every item of iterable,
yielding the results. If additional iterable arguments are passed, function
must take that many arguments and is applied to the items from all
iterables in parallel. With multiple iterables, the iterator stops when the
shortest iterable is exhausted."

Using a map defined with None raises
TypeError: 'NoneType' object is not callable

tjr

Jun 27 '08 #7

P: n/a
Paddy wrote:
On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?
My question is why you are looking up the semantics of Python functions on
Wikipedia instead of the Python documentation. I don't see any particular
discussion of map() there at all. Am I missing something?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #8

P: n/a
On Jun 12, 8:55 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>
And the OP's question was about map not being conforming to the
definition on wikipedia - which I don't think it's not. It is not
defined what map is to do with None (or NULL or nil or... ) as argument.

Diez
Oh no!
Sorry to give that impression. I don't think that map should be like
what Wikipedia says, I was just looking for another example of an
implementation that might mention the behaviour.

I just want to know the thoughts behind this behaviour in the Python
map.

- Paddy.
Jun 27 '08 #9

P: n/a
On Jun 12, 9:48 pm, Robert Kern <robert.k...@gmail.comwrote:
Paddy wrote:
On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

My question is why you are looking up the semantics of Python functions on
Wikipedia instead of the Python documentation. I don't see any particular
discussion of map() there at all. Am I missing something?

--
Robert Kern
As I said in an answer to Diez B. Roggish, I had been reminded of the
behaviour, thought it odd, and looked for other implementations that
might have the behaviour via wikipedia. My intension was most
definitely NOT to say that Pythons map should do what Wikipedia says
slavishly.

Sometimes when I pick at these seeming inconsistencies I learn a lot
from the c.l.p replies. Someone elses thread on -0.0 versus +0.0
taught me some more on floating point for example.

- Paddy.
Jun 27 '08 #10

P: n/a
On Jun 12, 9:36 pm, "Terry Reedy" <tjre...@udel.eduwrote:
"Paddy" <paddy3...@googlemail.comwrote in message

news:a8**********************************@27g2000h sf.googlegroups.com...
|
| Iam wondering why the peculiar behavior of map when the function in
| given as None:

The 'peculiar behavior' is the same as zip (except for padding short
iterators versus truncating long iterators. Map was added years before
zip. After that, map(None,...) was kept for back compatibility.

In 3.0, the doc for map is
"Return an iterator that applies function to every item of iterable,
yielding the results. If additional iterable arguments are passed, function
must take that many arguments and is applied to the items from all
iterables in parallel. With multiple iterables, the iterator stops when the
shortest iterable is exhausted."

Using a map defined with None raises
TypeError: 'NoneType' object is not callable

tjr
I really should get into the habit of reading the 3.0 docs before
asking questions :-)

My original question came about after answering this query:
http://gmcnaughton.livejournal.com/2...d=70451#t70451
- Paddy.
Jun 27 '08 #11

P: n/a
On Thu, 12 Jun 2008 12:05:02 -0700 (PDT), Paddy
<pa*******@googlemail.comwrote:
>
Iam wondering why the peculiar behavior of map when the function in
given as None:
If you start with a value x and then apply no function
at all to it, what results is x.
>Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) -list

Return a list of the results of applying the function to the items
of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length. If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).
It seems as the action whith none is the same as using a function of
lambda *x: x
As in the following example:
>>>l1 = 'asdf'
l2 = 'qwertyuip'
l3 = range(3)
l1,l2,l3
('asdf', 'qwertyuip', [0, 1, 2])
>>>map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)
True
>>>>


On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

Thanks, Paddy.
David C. Ullrich
Jun 27 '08 #12

P: n/a
On Jun 13, 12:49 pm, David C. Ullrich <dullr...@sprynet.comwrote:
On Thu, 12 Jun 2008 12:05:02 -0700 (PDT), Paddy

<paddy3...@googlemail.comwrote:
Iam wondering why the peculiar behavior of map when the function in
given as None:

If you start with a value x and then apply no function
at all to it, what results is x.

David C. Ullrich
True, but None is not a function. It's a sentinel value to turn on the
functionality.

- Paddy.
Jun 27 '08 #13

P: n/a
In article
<d5**********************************@j22g2000hsf. googlegroups.com>,
Paddy <pa*******@googlemail.comwrote:
On Jun 13, 12:49 pm, David C. Ullrich <dullr...@sprynet.comwrote:
On Thu, 12 Jun 2008 12:05:02 -0700 (PDT), Paddy

<paddy3...@googlemail.comwrote:
>Iam wondering why the peculiar behavior of map when the function in
>given as None:
If you start with a value x and then apply no function
at all to it, what results is x.

David C. Ullrich

True, but None is not a function. It's a sentinel value to turn on the
functionality.
Uh, thanks. I think I knew that - I was just suggesting why
the way map works makes sense.
- Paddy.
--
David C. Ullrich
Jun 27 '08 #14

P: n/a

"David C. Ullrich" <du******@sprynet.comwrote in message
news:du****************************@text.giganews. com...
| In article
| <d5**********************************@j22g2000hsf. googlegroups.com>,
| Paddy <pa*******@googlemail.comwrote:
|
| True, but None is not a function. It's a sentinel value to turn on the
| functionality.
|
| Uh, thanks. I think I knew that - I was just suggesting why
| the way map works makes sense.

filter(None, iterable) works the same way: None-identity function,
The immediate reason is the Python has no builtin id().
But apparently there is also historical precedent in the functional
community for this convention.

Jun 27 '08 #15

P: n/a
On Fri, Jun 13, 2008 at 2:00 PM, Terry Reedy <tj*****@udel.eduwrote:
filter(None, iterable) works the same way: None-identity function,
The immediate reason is the Python has no builtin id().
But apparently there is also historical precedent in the functional
community for this convention.
Another way of viewing it is that filter(None, iterable) applies no
function at all before testing the truth values, which does make some
sense. With map, however, this is not strictly true.
Jun 27 '08 #16

This discussion thread is closed

Replies have been disabled for this discussion.