-1

What?

I want to use a variable's name, not it's value. For instance, in the following example, I would like the function to return my_list[1] , and not B..

my_list = ['A', 'B']
def example(list_element):
   print(repr(eval(list_element)))

example(my_list[1]) # I would like this to print `my_list[1]`

But Why?

I am trying to create a function that takes a given element from a list, and also uses the previous list element. By getting the name my_list[1], I can subtract one and also get my_list[0]. Once I have both the names, I can utilise the values stored under these names.

Yes, I could simply add two fields to the function and put them in each time but I was hoping to keep the body of my code a little easier to read.

Solebay Sharp
  • 519
  • 7
  • 24
  • do you mean `print(f"{list_element=}")`? – Sayse Dec 16 '20 at 20:30
  • 5
    Objects don't know which variables they happen to be bound to at any given time. Are you planning on hardcoding this to only work with one specific variable "my_list" or should be be generic? – tdelaney Dec 16 '20 at 20:30
  • Is your goal to have a function that when given `my_list` and index 1, returns "A", the previous value? – tdelaney Dec 16 '20 at 20:32
  • This is a horrible idea. return a slice from the list or maybe a tuple of prev/act value if you need more then 1 value – Patrick Artner Dec 16 '20 at 20:32
  • I wish people wouldn't continue closing these types of beginner's questions by pointing to existing answers that send them down the road of eval hell. Sure, that's what they asked about literally - but it's clearly not what they need. – Grismar Dec 16 '20 at 20:35
  • 1
    @grismar What do you propose instead? leaving the same questions open? I put the "Dont do it" and advice of "return a slice from the list or maybe a tuple of prev/act value" in the comment. if we leave it open, the same bad crappy answers turn up - using eval, using inspect, search through global() etc – Patrick Artner Dec 16 '20 at 20:38
  • 2
    @PatrickArtner - no, providing a simple answer that goes into the difference between data and code. Mine below is about as simple an answer as OP needed. You could write a nicer answer that concisely explains the difference and results in a similar solution. What the referenced answers do is "sure, if you want to be an idiot: here's how" - they're not wrong, but they're no good to the beginning coders that just don't know any better and the answer will just send them further down a dead end. – Grismar Dec 16 '20 at 20:41
  • @Grismar the duplicate question linked is a 10-year-old answer with no example code and so I wasn't confident if I was asking the same question or facing the same hurdle (I thought not having a MRE was frowned upon). I also included the 'Why' part so people could nudge me towards the correct solution to my problem, this isn't present in the duplicate's answer (as a result of it having no example code). But alas I am downvoted nonetheless which will probably bury this question in the search results. I didn't think it was too bad. – Solebay Sharp Dec 16 '20 at 20:42
  • @PatrickArtner - I agreed that leaving it open would just cause more bad answers, but pointing to the bad answers, just reinforces the bad answers there. – Grismar Dec 16 '20 at 20:42
  • 1
    @SolebaySharp - that's good to hear, and means you're on your way to better things. My comment was more general, questions like these come up more than daily. But thanks for the feedback. I think we can leave this as it is :) – Grismar Dec 16 '20 at 20:44

1 Answers1

2

Don't use data to manipulate your code, it's not how Python (or most languages) works.

To do what you're trying to do:

my_list = ['A', 'B']


def example(a_list, index):
    print('The element passed: ', a_list[index])
    print('The element before it: ', a_list[index-1])


example(my_list, 1)

Of course this doesn't check if you didn't accidentally pass 0, etc. - but it shows you don't need to make a mess with eval, exec, etc.

Grismar
  • 27,561
  • 4
  • 31
  • 54