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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:42:26+00:00 2026-06-07T10:42:26+00:00

I really hope this isn’t a duplicate. I tried to search for my question

  • 0

I really hope this isn’t a duplicate. I tried to search for my question and I couldn’t seem to find it.

So I have a fairly simple function that converts feet to meters:

def feetToMeters(val):
    return numpy.array(val) * 0.3048

This works nicely and accepts ints, floats, arrays, and lists. However, if I put in a list (instead of a numpy array), I’d like to have a list returned. So I wrote this:

def feetToMeters(val):
    try:
        return val * 0.3084
    except TypeError:
        return [0.3084 * v for v in val]

(Alternatively I could use return list(numpy.array(val) * 0.3084) for the last line if I want to use numpy here, which I don’t know if that really matters.)

Is this the best way to incorporate duck-typing here so that I can avoid the use of the type function? Originally I tried AttributeError, but it didn’t work. Still, I am weary about TypeError even though it seems to work.

Would it be sacrilegious to use if type(val) is list instead?

  • 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-07T10:42:29+00:00Added an answer on June 7, 2026 at 10:42 am

    Would it be sacrilegious to use if type(val) is list instead?

    Yes, because it doesn’t work for subclasses of list. If you want to go this way, at least do isinstance(val, list). Here’s a solution that treats lists specially, and convert everything else (including scalars and tuples) to NumPy arrays:

    def feetToMeters(feet):
        meters = np.asarray(feet) * 0.3048
        return list(meters) if isinstance(feet, list) else meters
    

    Note that:

    • passing an instance of a subclass of list will cause a plain list to be returned;
    • passing a list of lists will cause a list of NumPy arrays to be returned.

    You could extend this to handle more types specially, but in general, for each type to be handled, you need to write more code as you need to know how to construct that type. Therefore, this kind of type conversion is usually left to client code.

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

Sidebar

Related Questions

I'm really new at network-programming, so I hope this isn't a complete Newbie-question. I
I really hope this isn't redundant, but I honestly can't find the answer in
I hope this isn't too simple, but I have a report retrieves rows using
I really hope this is not a question posed by millions of newbies, but
Hey everyone, this seems like it should be a simple one; I really hope
I really hope this isn't one of those super basic questions. Anyway, I've got
I'm still a newcomer to python, so I hope this question isn't inane. The
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
I hope this isn't a duplicate... What is the most solidly designed and implemented
I'm not really sure how to phrase my issue, so I hope this isn't

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.