0

I'd like to display the name of the variable being defined by a Python input statement. I'd like the display of the variable name to change with the name of the variable being populated by input.

Example:

S1 = input('Enter value for %variable%: ')

The expected output of the above would be:

Enter value for S1:

Can this be done?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dan O'Boyle
  • 3,676
  • 5
  • 28
  • 44
  • 1
    Why would you want to do that? The string literal expression runs before the `S1` assignment anyway, and assignments can be to lists or dictionaries or attributes, where you don't have a name as such. – Martijn Pieters Feb 28 '15 at 19:13
  • `s1 = input('Enter a value for s1: ')` -- you are the one writing these names, no?! – Two-Bit Alchemist Feb 28 '15 at 19:13
  • @Two-BitAlchemist Pretend that this statement happened Many, Many times and you wanted standard code to display whatever variable was being populated. – Dan O'Boyle Feb 28 '15 at 19:14
  • 1
    @DanO'Boyle: then use a dictionary and a loop: `for name in variable_names: values[name] = input('Enter value for {}: '.format(name))` – Martijn Pieters Feb 28 '15 at 19:15
  • @DanO'Boyle I would be using a dictionary then and mapping the string names to the input values. – Two-Bit Alchemist Feb 28 '15 at 19:15
  • @MartijnPieters That's the heart of my question - I'd like to know if there's a way to gather information about the variable being populated before the input statement. Specifically the name of the variable. – Dan O'Boyle Feb 28 '15 at 19:16
  • @DanO'Boyle You are the one coming up with the variable name! You don't have to ask the computer what you are writing. In what sense is "the variable being populated" by the computer at all? Where are the names you are asking about coming from if not your own head? – Two-Bit Alchemist Feb 28 '15 at 19:17
  • Just because this is poor practice, I don't think we should be downvoting his question. Instead, why not just help and show him the better alternative? – Dair Feb 28 '15 at 19:19
  • @DanO'Boyle: no, not without parsing the source code of that line. The problem is far easier avoided altogether. – Martijn Pieters Feb 28 '15 at 19:19
  • Thanks @Bair That's what I'm looking for. if there's something wrong at the heart of my question - I'm happy to hear better ways to do it. – Dan O'Boyle Feb 28 '15 at 19:22
  • I could see the variable variables question being relevant. Rather though I think that question explains a best practice to a method of solving my problem, rather than answering the question. – Dan O'Boyle Feb 28 '15 at 19:27
  • I would accept @MartijnPieters response about using a dictionary, if posed as an answer. I'd accept zachgates response if not for the discussion on globals. – Dan O'Boyle Feb 28 '15 at 19:31

1 Answers1

3

Extracting the assignment target is hard. You'd have to parse the actual source code of the current module, perhaps using the ast module. The information is not normally available in Python code, as the right-hand side is executed before assigning.

Use a dictionary and a list of variable names instead:

variable_names = ('S1', 'S2', 'foo', 'bar')
values = {}

for name in variable_names:
    values[name] = input('Enter value for {}: '.format(name))

This is more convenient than using local variable names, because you can now pass around just these names, and not have to filter out any other local variables.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    There's a caveat about this: the people running the program may not be able to deduce from the variable names what the purpose of the variable is. This might be OK for the original programmer, or if the variable names are sufficiently self-descriptive (that rules out `foo` and `bar`, and without knowing more about the problem space, leaves `S1` and `S2` in the dubious category), but it is not going to be useful for someone who doesn't know what the variables mean — that means any ordinary non-programmer end-user, or even a programmer who didn't write or maintain the code. – Jonathan Leffler Feb 28 '15 at 19:38
  • @JonathanLeffler: sure, the question remains devoid of all context and separate instructions are still needed. But that's not what the question was really about. :-) – Martijn Pieters Feb 28 '15 at 19:41