1

Why are some in-built functions, like range, map and zip, of type type, while other functions, like, eval, repr and sum, of type builtin_function_or_method?

I understand that int, float, list, etc. are type type because they are implemented as classes. But range, map and the sort are functions which return iterables. Are these also implemented as classes? If so, why?

Edit: I just noticed that type(range(0,1)) is range and type(map(func,arr)) is type map. So they are indeed implemented as classes.

What advantage does this implementation have over implementing them as list?

SvbZ3r0
  • 638
  • 4
  • 19
  • The `range` and `map` functions return objects of those types. `range` in particular isn't just an ordinary iterable. – TigerhawkT3 Jun 24 '16 at 07:37
  • Yes, I just noticed that. Why is this so? – SvbZ3r0 Jun 24 '16 at 07:38
  • 2
    Custom types may be lazy while being 100% conformant with "iterable" interface. Since they are very often used in iterating context, there is an immediate decrease in memory usage when laziness is applied. It's basic reason for switching to lazy sequences in common built-in functions in Python 3. – Łukasz Rogalski Jun 24 '16 at 07:40
  • @Rogalski I'm really sorry, but when do you call something **lazy**?? – SvbZ3r0 Jun 24 '16 at 07:43
  • Lazy means they compile but don't calculate until actually called during runtime – Ic3fr0g Jun 24 '16 at 07:46
  • @MayurH Like when you use `yield`? – SvbZ3r0 Jun 24 '16 at 07:49
  • 2
    @SvbZ3r0 yes, exactly - lazy evaluation basically means *"not calculated until (and unless) actually needed"* – jonrsharpe Jun 24 '16 at 07:53
  • 1
    Yes, in Python one of essential lazy types is `generator`. Another lazy type is `range`. Related: http://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3 – Łukasz Rogalski Jun 24 '16 at 07:54
  • @Rogalski Thank you. So, `map` and `zip` essentially work the same way too? Or are they unrelated? – SvbZ3r0 Jun 24 '16 at 08:02

0 Answers0