0

I can not understand how and why the first time it is printed a reference of the object(which object is referring to?) and the second time when I use two variables, these variables get the result of the function instead of a reference.

>>> a = map(int,[1,2])
>>> a
<map object at 0x7f0b1142fa90>

>>> b,c = a
>>> b
1
>>> c
2
1
  • 1
    That has nothing to do with map, it's how the language was designed. The iterator is unpacked to the variables b and c. Commented Jun 20, 2018 at 21:04

1 Answer 1

1

In Python 3, map (and other primitive combinators) return an iterator object rather than a list (as they did before.) At the first attempt, you printed that iterator object per se, while the second time you matched it against a sequence, thus forcing and extracting elements. Consider:

>>> a = map(int,[1,2])
>>> a
<map object at 0x7ff6ddbfe748>
>>> list(a)
[1, 2]
Sign up to request clarification or add additional context in comments.

9 Comments

Can you explain primitive combinators? I'm not familiar with this term.
@jpp see functions like map, zip, etc. Many more of these are defined in the itertools library, and they are very common in functional languages. The general way to think of these is that you have some generic action you want to take on one or more lists, and the combinators provide a generic way to do it. I.e. mapping an arbitrary function lazily over an iterator (map).
@jpp I've meant simple (standard) functions that operate on sequences and accept a function as argument; probably 'sequence processors' would be a better term. Of Python's built-in functions we can name at least map and filter. zip and reversed, e.g., work the same way: they used to return lists before, but now return iterators.
@bipll, Thank you. I get what it covers. I just don't understand the term primitive combinators. How is map "primitive" or a "combinator"? I think combinator is a term used in functional programming for a type of higher order function. Primitive I can't really pinpoint.
I don't think reversed used to return a list did it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.