Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:29:47+00:00 2026-06-16T01:29:47+00:00

I just came across this strange behaviour of numpy.sum : >>> import numpy >>>

  • 0

I just came across this strange behaviour of numpy.sum:

>>> import numpy
>>> ar = numpy.array([1,2,3], dtype=numpy.uint64)
>>> gen = (el for el in ar)
>>> lst = [el for el in ar]
>>> numpy.sum(gen)
6.0
>>> numpy.sum(lst)
6
>>> numpy.sum(iter(lst))
<listiterator object at 0x87d02cc>

According to the documentation the result should be of the same dtype of the iterable, but then why in the first case a numpy.float64 is returned instead of an numpy.uint64?
And how come the last example does not return any kind of sum and does not raise any error either?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:29:48+00:00Added an answer on June 16, 2026 at 1:29 am

    In general, numpy functions don’t always do what you might expect when working with generators. To create a numpy array, you need to know its size and type before creating it, and this isn’t possible for generators. So many numpy functions either don’t work with generators, or do this sort of thing where they fall back on Python builtins.

    However, for the same reason, using generators often isn’t that useful in Numpy contexts. There’s no real advantage to making a generator from a Numpy object, because you already have to have the entire Numpy object in memory anyway. If you need all the types to stay as you specify, you should just not wrap your Numpy objects in generators.

    Some more info: Technically, the argument to np.sum is supposed to be an “array-like” object, not an iterable. Array-like is defined in the documentation as:

    An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.

    The array interface is documented here. Basically, arrays have to have a fixed shape and a uniform type.

    Generators don’t fit this protocol and so aren’t really supported. Many numpy functions are nice and will accept other sorts of objects that don’t technically qualify as array-like, but a strict reading of the docs implies you can’t rely on this behavior. The operations may work, but you can’t expect all the types to be preserved perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

fellow coders, just came across a strange (at least to me) behaviour for the
Just came across this by Chris Coyier - http://css-tricks.com/examples/CSSTabs/ Can anyone explain me, how
I've just came across this on GitHub. ($config === NULL) and $config = Kohana::config('email');
I'm new to C programming and I just came across this program and got
I came across this term - Quine (also called self-reproducing programs). Just wanted to
I came across this link flying twitter bird , was just wondering , how
We just came across this in an old production stored proc (there is a
I just came across this weird problem that is happening in IE. In the
I'm currently working my way through Accelerated C++ and just came across this in
Hello my fellow Stackoverflownians :), I just came across this thing called Zend. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.