0

Previously I asked a question here. That problem solved but there are error while developing the script.

Currently, an option is obtained from command line like:

... -b b1

the code processing that is:

import Mybench, optparse
parser.add_option("-b", "--benchmark", default="", help="The benchmark to be loaded.")  
if options.benchmark == 'b1':
   process = Mybench.b1
elif options.benchmark == 'b2':
   process = Mybench.b2
...
else:
   print "no such benchmark!"

Now I have changed so that more than one option is passed to "-b".

... -b b1,b2

The code for this is:

process = []
benchmarks = options.benchmark.split(',')
for bench_name in benchmarks:
   print bench_name
   process.append(Mybench.bench_name)

If you notice, in the first code the process gets value like this:

process = Mybench.b1

Now I write this:

process.append(Mybench.bench_name)

I expect that this command line:

... -b b1,b2

results in:

process[0] = Mybench.b1
process[1] = Mybench.b2

However I get this error:

process.append(Mybench.bench_name)
AttributeError: 'module' object has no attribute 'bench_name'

Is there any solution to that?

2 Answers 2

4

bench_name is a string, not an identifier, so you need to use getattr():

process.append(getattr(Mybench, bench_name))
Sign up to request clarification or add additional context in comments.

Comments

2

For me there's a difference between :
- process.b1
- process.bench_name => process."b1"

getattr() may be the key of your wills.

Comments

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.