473,799 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mapping None. Why?


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
15 1384
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
On Thu, Jun 12, 2008 at 1:05 PM, Paddy <pa*******@goog lemail.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
On Thu, Jun 12, 2008 at 1:32 PM, Diez B. Roggisch <de***@nospam.w eb.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
Ian Kelly wrote:
On Thu, Jun 12, 2008 at 1:05 PM, Paddy <pa*******@goog lemail.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
Ian Kelly schrieb:
On Thu, Jun 12, 2008 at 1:32 PM, Diez B. Roggisch <de***@nospam.w eb.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

"Paddy" <pa*******@goog lemail.comwrote in message
news:a8******** *************** ***********@27g 2000hsf.googleg roups.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
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
On Jun 12, 8:55 pm, "Diez B. Roggisch" <de...@nospam.w eb.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
On Jun 12, 9:48 pm, Robert Kern <robert.k...@gm ail.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1905
by: max | last post by:
I am trying to use a sdk to access quickbooks, the sdk provided a COM interface and I think it installed the com server files/dlls. A VB code example is provided, but I can't figure out how to map "Dim sessionManager As New QBFC2_1Lib.QBSessionManager" into a corresponding Python statement, when I try obj = win32com.Client.Dispatch("QBFC2_1Lib.QBSessionManager") I get traceback Shown below. I have been able to run makepy and it...
9
2477
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma. I am referring to (from the manual); Slice objects Slice objects are used to represent slices when extended
1
1716
by: none | last post by:
Hi, I'm trying to establish table mappings, and I've hit a snag. At the point to where I try to fill the schema (DB_adapter.FillSchema), I get an exception, and the message is as follows: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Invalid column name 'Unique_ref'. Invalid column name 'ID_string'. Invalid column name 'Sequence'.
8
1380
by: micklee74 | last post by:
hi i wish to map None or "None" values to "". eg a = None b = None c = "None" map( <something> , if i in ("None",None) ]) I can't seem to find a way to put all values to "". Can anyone help?
3
1305
by: Lee Sander | last post by:
hi, I have the following problem which is turning out to be non-trivial. I realize that this is not exactly a python problem but more of an algorithm problem -- but I post it here because I want to implement this in python. I want to write a code that given an interval (integer tuple: start,stop) will find which other interval it matches to. Here is an example,
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.