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 ?

4
  • 1
    1) Huh? I can't understand this code. did you import config and it has variables like config.pydec_pair? 2) this code design seems fishy and eval us unsafe. 3) did you mean to initialize the arrays like numpy.zeros(200) instead of numpy.array(200)? Commented Jul 23, 2012 at 18:51
  • ok I made too many typos in this question :| ..yes I meant to initialize it like nump.zeros(200), made a mistake while typing ..I want to access it and modify it from the module roc.py Commented Jul 23, 2012 at 18:56
  • But... are there variables like config.pydec_pair? Or what? What's the point of the dosomething function? Commented Jul 23, 2012 at 18:57
  • I want to be able to modify the arrays dec_pair and dec_community from the module roc.py , which receives the name of the array to be modified on as an input. Therefore, I am trying to access the correct array based on the parameter name. Maybe I did this horribly wrong .. what is the correct method to achieve it ? Commented Jul 23, 2012 at 19:02

2 Answers 2

5

Yuck.

First of all, don't use eval(). You're gonna have a bad time.

Secondly, just import the config! In roc.py:

def dosomething(numpy_array):
    return somefunction(numpy_array)

In some other module:

import roc
import config
someresult = roc.somefunction(config.dec_pair)

Edit: After re-reading your question, I believe you're trying to use global values. This is almost always an indication of poor design. You should restrict your use of the "global" variables to a single script, and use functions to pass those objects around.

Sign up to request clarification or add additional context in comments.

3 Comments

Colin: I thought it was implied that I was obviously importing config. I have edited my code to reflect that. dosomething() gets called from a third module say foo.py, which passes it the argument name
@R.Bahl don't pass names. Pass objects.
+1 for "don't use eval(), you're gonna have a bad time ..."
3

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.

11 Comments

+1 to everything except the globals and vars bit. Don't suggest that, it's bad practice.
@ColinDunklau -- Agreed. I added a statement saying "Please don't do this". However, I have used those functions once or twice (and only once or twice) so I think it's good to know that they are there and what exactly they do.
OK I get that ! I will modify my design to take into account your suggestions ! Thanks Colin and mgilson !
Can you comment as to why it is actually not good to use them ? why do they exist and situations where they can be useful ?
@R.Bahl Global variables detract from readability and logical separation. More to the point, code that uses global variables can be absolute hell to debug, since it's not easy to identify where they the value comes from. In my (limited) experience, they're only useful in the debugger.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.