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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:46:13+00:00 2026-06-08T06:46:13+00:00

I have two numpy arrays named dec_pair and dec_community in a module named config.py

  • 0

I have two numpyarrays named dec_pair and dec_community in a module named config.py, initialized to zero:

dec_pair = numpy.zeros(200)
dec_community = numpy.zeros(200)

Now, I am trying to access them from some other module, say roc.py, where their names are being formed based on a input variable, i.e.

import config
def dosomething(name):
    local_name = 'config.py'+name
    eval(local_name)[i:] += 1

where name can be pair or community. The issue is, eval(local_name) is returning the length of the numpy array i.e. 200 here and not the array itself, which gives me this error:

ValueError: cannot slice a 0-d array

However, when I do the same thing on the python interpreter, it runs smoothly :

>>> dec_pair = numpy.zeros(5)
>>> name = 'pair'
>>> local_name = 'dec_'+name
>>> eval(local_name)
array([ 0.,  0.,  0.,  0.,  0.])

Any idea as to what I am doing wrong and what would be the correct way to do it ?

  • 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-08T06:46:15+00:00Added an answer on June 8, 2026 at 6:46 am

    What you’re trying to do looks like a bad idea (although I’m having a hard time figuring out what you’re trying to do).

    My first instinct is to say that you probably want numpy.zeros(200) instead of numpy.array(200) in your config file.

    Second, if this stuff is imported, you could probably use globals()[local_name] or vars(config)[local_name] to get your data rather than eval, although that is still definitely bad practice (in other words, Please don’t do this — see point 3).

    Third, when you don’t know the name of the variable ahead of time, you should be using a dictionary to begin with e.g.

    #config.py
    dec={'pair':np.zeros(200), 'community':np.zeros(200)}
    

    Now you just access them in your function as:

    a=config.dec[name]
    a[i:]+=1
    

    If you wanted an easier handle on dec_pair and dec_community, you could do config.py like this:

    dec_pair=np.zeros(200)
    dec_community=np.zeros(200)
    dec={'pair':dec_pair, 'community':dec_community}
    

    Although I would probably say that it’s best to keep the data referenced in only 1 place so I would caution against doing this unless you really need to keep an API consistent or something.

    Finally, you haven’t mentioned what the value of the variable i is — accessing variables outside of the current namespace is often risky, and variables named i are probably even more risky.

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

Sidebar

Related Questions

I have two numpy arrays that define the x and y axes of a
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]
I have two equal-length 1D numpy arrays, id and data , where id is
I have two numpy arrays that are OpenCV convex hulls and I want to
I have two NumPy arrays, e.g.: a = [1,2,3,4,5] and a filter array, e.g.:
I have two Numpy record arrays that have exactly the same fields. What is
I have two numpy arrays X and W each with shape (N,N) that result
I have two sliced Numpy arrays stored in memory, say a and b, shape
I have two equally sized numpy arrays (they happen to be 48x365) where every

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.