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

  • SEARCH
  • Home
  • 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 9257489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:10:21+00:00 2026-06-18T12:10:21+00:00

When calling a function that accepts a list, who is responsible (caller – a

  • 0

When calling a function that accepts a list, who is responsible (caller – a user, or called – the function) to ensure that it is a list and not a generator?

An example:

>>> def print_collection(coll):
...     for element in coll:
...         print element

>>> def print_collection_twice(coll):
...     print_collection(coll)
...     print_collection(coll)

With a list it will work without surprises:

>>> print_collection_twice( [x*2 for x in xrange(3)] )
0
2
4
0
2
4

And with a generator, obviously it is printed only once, which might lead to a nasty bug:

>>> print_collection_twice( (x*2 for x in xrange(3)) )
0
2
4

What is the best practice here? Should a function assume a list, and a user is responsable to provide the list, or should the function do always real_list = list(input_list) at the beginning so the user does not care?

Edit

I know how to check type of the element and assert, my question is rather high level

  • 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-18T12:10:22+00:00Added an answer on June 18, 2026 at 12:10 pm

    Either approach is defensible. It is the responsibility of the function to document what kinds of arguments it wants, and the responsibility of the caller to pass arguments consistent with the documentation. If the function says it wants a list and you pass a generator, there are no guarantees it will work.

    The real question is what the function should say it wants, and the answer is that it should say just what it needs, and no more. So don’t say you need a list if all you really need is an iterable. In general, if your function needs to use features of lists that general iterables don’t have (e.g., indexing), then it should just use those features, and an exception will naturally be raised if someone passes in an argument that doesn’t support them. If your function doesn’t need those features, then it doesn’t need a list.

    Your example is somewhat unrealistic because all it does is print the argument. In real life you would almost always need to do something besides just consume the iterable, and the nature of that “something you need to do” would clarify what sort of argument you should accept. For your specific example, though, I would say yes, call list on it (inside print_collection_twice, not inside print_collection). The reason is that print_collection_twice wants to use the data more than once, which is not possible for a generic iterable.

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

Sidebar

Related Questions

While searching a solution for calling a function that accepts both lvalue and rvalue
I have a function that is recursively calling itself, and i want to detect
I have a function that is calling wget via subprocess.Popen. The purpose of this
I am calling a function and passing 3 parameters to that. I am taking
I have the view function in django that written like a dispatcher calling other
When calling functions that always throw from a function returning a value, the compiler
Is it possible that when calling multiple times this function: void Func(String spr, PictureBox
I have a PHP script that I'm calling with the .post() function in jQuery.
Does anyone know how to watch a variable in calling function. For example: C#:
Say I have a template function that accepts variable arguments and calls other functions

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.