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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:52:58+00:00 2026-06-11T17:52:58+00:00

I am learning Python and numpy, and am new to the idea of duck

  • 0

I am learning Python and numpy, and am new to the idea of duck typing. I’m writing functions into which something/someone should pass a numpy array. Trying to embrace duck typing, I’m writing my code to use numpy.array with the copy= and ndmin= options to convert array_likes or 1d/0d arrays into the shape I need. Specifically, I use the ndmin= option in cases where I can accept either a (p,p) array or a scalar; the scalar can be coded as an int, (1,) array, (1,1) array, [1] list, etc…

So to take care of this, I’m using something like S = numpy.array(S,copy=False,ndmin=2) to get an array (if possible) with the right ndim, then test for the shape as I need. I know I should embed this in a Try-Except block, but can’t find any documentation about what kind of exception numpy.array() is likely to throw. Thus I currently just have this:

# duck covariance matrix into a 2d matrix
try:
    S = numpy.array(S, ndmin = 2, copy=False)
except Exception as e:
    raise e

What specific exception(s) should I try to catch here? Thanks.

  • 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-11T17:52:59+00:00Added an answer on June 11, 2026 at 5:52 pm

    Document your function as accepting an array_like object and leave handling of exceptions to a caller.

    numpy.array() is very permissive function it will convert to an array almost anything.

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

Sidebar

Related Questions

I learning Python (coming from a dotnet background) and developing an app which interacts
I just started learning Python... I am writing a simple program that will take
I'm learning Python, and I am up to the chapter on functions, and I
I am learning python and i am new bie. I am trying to use
I am learning python, now, i came across a code snippet which looks like
I'm learning Python, and have run into a bit of a problem. On my
I am learning python via dive into python. Got few questions and unable to
So I'm learning Python. I am going through the lessons and ran into a
I have just started learning python version 3 and trying to create a file
I have only started learning python recently. I would still be considered a beginner.

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.