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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:49:01+00:00 2026-06-11T18:49:01+00:00

I would like to perform checks on the contents of a numpy array every

  • 0

I would like to perform checks on the contents of a numpy array every time it is set. Can python properties be used for this? My approach:

import numpy as np

class Obj(): 
    def __init__(self):                                                                                 
        self._np_arr = None                                                                             

    @property                                                                                           
    def np_arr(self):                                                                                   
        if self._np_arr is None:                                                                        
            self._np_arr = np.ones(10)                                                                  
        return self._np_arr                                                                             

    @np_arr.setter
    def np_arr(self, value):
        if np.sum(value)>10:
            raise ValueError('Error message')                                                           
        self._np_arr = value

if __name__ == '__main__':
    o = Obj()                                                                                           
    print o.np_arr
    o.np_arr = np.zeros(10) # ok                                                                        
    o.np_arr = np.ones(10)*2 # not ok                                                                   
    print o.np_arr       

The getter is entered when the object is still None. Once np_arr is a numpy array the getters and setters no longer work.

What am I doing wrong?

  • 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-11T18:49:02+00:00Added an answer on June 11, 2026 at 6:49 pm

    First of all. This is difficult (up to, I believe impossible) to do right. The error here is quite simple, it must be a new style class. So just replace class Obj() with class Obj(object).

    This may not solve your problem however, since the user can still assign it in place, ie:

    o.np_arr[:] = 2
    

    You could circumvent that somewhat by using _np_arr.setflags(write=False), but then the user cannot use such operations at all.

    Edit:
    you could also subclass ndarray, and define your own __array_finalize__ to circumvent that probably. However, even then the user could do np.asarray(o.np_arr) and modify that in place…

    I guess you could provide your own methods then “simulate” an array by wroking internally on a writeable one. But I doubt there is a 100% foolproof or at least elegent method.

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

Sidebar

Related Questions

I would like to perform something similar to this (ie get the sum of
I would like to perform a bitwise exclusive or of two strings in python,
I would like to perform @Range based hibernate validation checks (org.hibernate.validator.RangeValidator) in my JPA
What is a tool or technique that can be used to perform spell checks
I would like to programmatically perform a check on the APK's signature at runtime.
I would like to perform 5 consecutive reads to a slave devices and check
I would like to perform a different action when my app moves to the
I would like to perform a small operation on all entities of a specific
I would like to perform a regular expression in jQuery to find all the
I would like to perform a progress indicator during request. I have try function

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.