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
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
OG_
  • 49
  • 6
  • 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. – cs95 Jun 20 '18 at 21:04

1 Answers1

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]
bipll
  • 11,747
  • 1
  • 18
  • 32
  • Can you explain `primitive combinators`? I'm not familiar with this term. – jpp Jun 20 '18 at 21:12
  • 1
    @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`). – BowlingHawk95 Jun 20 '18 at 21:18
  • @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 Jun 20 '18 at 21:18
  • @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. – jpp Jun 20 '18 at 21:22
  • @jpp Yes but I had `map` and `filter` in mind. They are very basic to be called 'primitive'. – bipll Jun 20 '18 at 21:23
  • @bipll what exactly is an iterator object??? I mean it's not a list or tuple.... what is its form?? I mean lists are like this [1,2,3] while tuples are like (1,2,3) and also these have some functions and operations to work with them.... what is the case with an iterator object – OG_ Jun 20 '18 at 21:32
  • 2
    I don't think `reversed` used to return a list did it? – Chris_Rands Jun 20 '18 at 21:38
  • @OG_ read this https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do – Chris_Rands Jun 20 '18 at 21:39
  • @OG_ Here's the brief explanation: https://docs.python.org/3.6/glossary.html#term-iterator It is a value that is returned by `iter()` (and had been in Python2 as well). It allows to traverse the whole iterable by retrieving element-by-element, and throwing an exception in the end. Behind the scenes, this is how Python's built-in constructs work, like `for ... in `, list comprehension, etc. – bipll Jun 20 '18 at 21:40