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 3423726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:23:22+00:00 2026-05-18T06:23:22+00:00

I can think of two ways to determine whether an object is a sequence:

  • 0

I can think of two ways to determine whether an object is a sequence:

  • hasattr(object, '__iter__').
  • And whether calling iter(object) raises a TypeError.

As it is most Pythonic to ask forgiveness than to ask permission, I’d use the second idiom, although I consider it more ugly (additionally, raising an exception once you’ve caught the TypeError to determine that the object isn’t a sequence would yield an undesirable “double-exception” stack trace).

Ultimately, is checking that an object defines an __iter__ method exhaustive enough to determine whether an object is a sequence? (In older versions of Python, for example, str didn’t define an __iter__ method; I’ve also heard that some objects can also simply define and use __getitem__ without defining an __iter__ and act like a sequence.) Or is defining __iter__ the contract of a sequence?

  • 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-05-18T06:23:23+00:00Added an answer on May 18, 2026 at 6:23 am

    Use isinstance(obj, collections.Sequence). Abstract base classes are exactly for this. They didn’t exist prior to 2.6 though. In case you’re forced to use older versions, you’re out of luck and better stick with EAFP.

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

Sidebar

Related Questions

I can think of a few messy ways to solve this, but it strikes
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup I can think of a few ways of doing it with loops but
I have a class A that can be generated from two different ways. a
I can think of plenty of good reasons to using it; however, what are
I've gone through just about every property I can think of, but haven't found
I'm sure there are different approaches to this problem, and I can think of
can anyone think of a better way to do this? [AcceptVerbs(HttpVerbs.Post)] public ActionResult SaveAction()
Can anyone think of a good solution for getting IOC into a console application?
I can't think of many reasons (or any at all) in which multiple independent
I can only think of Peek() and ReadNoAdvance() atm, but I wonder if there

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.