user6570767 user6570767 - 3 months ago 7
Python Question

What is a better way to manipulate named values?

I am thinking about, basically, a way to store data. Something like a table, if Python had tables. For example's sake, I have a list of names that I want to associated with changing values and then manipulate them in expressions, preferably by iteration. Here is an example: three names "foo", "bar" and "baz" are declared, decremented, then changed and again decremented (in my project, I have lists with a dozen names but the principle is the same).

Way 1: variables and if-elif-else:

foo = 123
bar = 456
baz = 789

def chooser(n):
if n == 0:
return foo - 1
elif n == 1:
return bar - 1
elif n == 2:
return baz - 1

for i in range(3):
print chooser(i)

foo = 1
bar = 2
baz = 3

for i in range(3):
print chooser(i)


Way 2: dictionary

data = {'foo':123, 'bar':456, 'baz':789}

for i in ('foo', 'bar', 'baz'):
print data[i] - 1

data['foo'] = 1
data['bar'] = 2
data['baz'] = 3

for i in ('foo', 'bar', 'baz'):
print data[i] - 1


However, I strongly suspect there is an even better and more pythonic way to do the same thing. Can the community please come with further alternatives, if there are any?

Answer

Yeah, there is a more pythonic way. It's called using .items(). Seriously dude, can't you try and look up dict in the Python official docs?

for key, value in data.items():
    print(data[key] - 1)

Old Answer

Use a dictionary. It can map strings to other objects, like so:

>>> data = {'foo' : 123, 'bar' : 456, 'baz' : 789}
>>> data['foo']
123

Your second way doesn't even work, even though you say it works. You just pretended like it does work. You don't know how to fundamentally use a dictionary.


Old Old Answer

  1. It's pythonic, not "pythonista". Pythonista describes somebody experienced with Python.
  2. Don't use global variables from within functions! I see no reason why you couldn't pass foo, bar, and baz as arguments.
  3. I don't know where you got "...putting in a tuple or dictionary but apparently it will not work at all" from. It will work. Just don't use a dictionary because that's not what you want. You can access a tuple's elements using tuple_name[array_index]. Remember: array indices start at zero. To create a tuple in your case you'll need (123, 456, 789)
  4. It's still unclear what you're trying to achieve here. Tell us what you want to achieve. Why even use the function? Why is it called chooser? Why not loop over a tuple like this? :

    data = (123, 456, 789)
    for element in data:
        print(element - 1)
    

Old Old Old Answer

I think you have a basic misunderstanding of the concepts in that code snippet.

foo, bar, baz = 123, 456, 789 does not create a list, it assigns 123 to foo, 456 to bar, and likewise for baz.

Your looping over range(2) - this is a loop! It'll go over each of the values 0-1, so 2 will never be reached and what's your 'illegal input' thing doing there?

You have forgotten the argument list in your functioon definition - required for any function, even if it has no arguments. It should be def chooser():.

And by the way, the ^ operator isn't exponentiation - it's bit shift, which is an advanced concept you shouldn't be learning now. To do exponentiation you must use the ** operator.

And what did you expect? chooser() will always return 123^2. (which is 121, the result of the bit shift)

It's unclear what your trying to achieve. Please clarify this in your question.

chooser does not ask the user for input, as the name would imply. I think you misunderstood that range() gets input. It doesn't: it returns an iterable (0, 1).

If you wanted to store some variables in a tuple you could write data = (123, 456, 789). Then instead of writing foo, you'd write data[0], and instead of writing bar, you'd write data[1]. Etc. for baz.

Comments