-3

I am using Python 2.5. In the code below want to print either err1, err2 or err3 depending on the value of x. Is it possible to create the variables err1, err2 or err3 by joining "err" plus the value of x?

err1 = 'error foo1' err2 = 'error foo2' err3 = 'error foo3' x = 1 print err + x 
5
  • 1
    Why are you using Python 2.5 and not 2.7? Commented Apr 22, 2014 at 18:20
  • 2
    @RobWatts: many school systems are outdated? Not that the precise version of Python matters here. Commented Apr 22, 2014 at 18:21
  • 2
    You want to use a dictionary or a list. Creating variables on the fly is a bad bad bad idea. Commented Apr 22, 2014 at 18:23
  • see mail.python.org/pipermail/python-list/2004-April/262316.html Commented Apr 22, 2014 at 18:38
  • 1
    If I were (still) a teacher, I would take points off for any code that tried this kind of dynamic variable generation. Use a dictionary. Commented Apr 22, 2014 at 18:50

6 Answers 6

3

You'd be better off re-writting it to use a dictionary if you want variable variable names.

err = { '1': 'This is Err1', '2': 'This is Err2', '3': 'This is Err3', } print err[str(x)] 
Sign up to request clarification or add additional context in comments.

2 Comments

Why use str to create dictionary keys? Can't they be ints too?
@SamMussmann They can be anything hashable. I used strings in this instance because I have no idea how else he's going to be getting/generating these keys, and thus far he's using string concatenation. If he's looking at adding 1 to err to make err1, he may be looking to adding 1 to that to make err11.
2

While what you want to do is possible, it's not recommended. Instead, use a dictionary:

err={} err[1] = 'error foo1' err[2] = 'error foo2' err[3] = 'error foo3' print err[x] 

While dynamically creating and referencing variables on the fly are an interesting aspect of many scripting languages, there are almost always to accomplish the same thing in a more straight-forward manner.

2 Comments

I'm not sure I fully understand your answer. You can easily convert a integer to a string in any non-scripting language. And why is repeating 'error foo' thrice better than concatenating?
The question isn't "how do I print a string followed by a number", the question was about how to dynamically reference a variable. It's not better to repeat "error foo" three times, but that's not what I think is being asked.
1

It is not really recommended, but you want:

x = 1 print getattr(__import__(__name__), 'err' + str(x)) # 'error foo1' 

Comments

0

Yes. Using str():

print 'err' + str(x) 

But as pointed by others in the comments, constructing a variable name on the fly by concatenating strings is not a good way to do things. Instead define a dictionary like:

error_codes = {} error_codes[1] = 'error foo1' error_codes[2] = 'error foo2' error_codes[3] = 'error foo3' 

Now based on value of x do:

print error_codes[x] 

5 Comments

This isn't exactly what they are asking. They aren't asking how to join strings, but rather how to reference variables in a dynamic fashion.
Also, the use of str() in str('err') has nothing to do with err (without quotes) not being defined, it's completely unecessary. If err wasn't defined, and you'd use str(err), you'd still get a NameError.
@LukasGraf I use str('err') and not str(err). Could you please read my answer in its entirety?
Yes. And it makes no sense (it's a no-op), and the justification you give for it is incorrect and misleading: It's entirely unrelated to a local variable named err.
@LukasGraf I mixed up a few things. Agreed. Thanks.
0

If you absolutely have to, you could use locals() to access local variables by a dynamic name (or globals() for that matter).

For example:

foo0 = 'a' foo1 = 'b' foo2 = 'c' for i in range(3): name = 'foo%s' % i value = locals().get(name) print value 

However, for locals() this should be considered read only use. Modifying values in locals() may seem to work in the interactive interpreter, but is unreliable. (Modifying values in globals() is reliable as far as I know, but I'd strongly advise doing that in production code.)

If the dynamic names you want to use are attributes on objects however, you can use getattr(obj, name) and setattr(obj, name, value), which are equivalent to value = obj.foo and obj.foo = value respectively. Those are "safe" and would work reliably. However, because it makes code very hard to read and understand without running it, they should be used sparingly for this purpose.

Comments

-3

I suppose that you want to simply display the string along with the integer value. So simply try using format specifiers for printing/displaying purpose:

For eg in your case: print 'err%d'%x

4 Comments

No, this will print "error 1". They don't want that. They want the equivalent of doing print err1
Edited the answer for exact output
You still aren't answering the question that was asked. Ignore what's in the variables. Assume you have three variables named err1, err2 and err3 and you have no idea what's in them. Given the number 1, how do you reference err1? that's the question being asked IMO.
You are right and I am left. I misunderstood the question. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.