0

In Python 3, if you have a class, like this:

LETTERS = list('abcdefghijklmnopqrstuvwxyz')
class letter:
    def __init__(self, name):
        self.number = LETTERS.index(name)
        self.nextLetter = LETTERS[self.number+1]
        self.previousLetter = LETTERS[self.number-1]

and you create an instance of letter, myLetter, like this:

myLetter = letter('c')

In letter, how do I get the variable name? (myLetter in this case)

nedla2004
  • 1,115
  • 3
  • 14
  • 29
  • I'm not following. You created an instance of `letter` (you should capitalize your class names -> `class Letter`) called `myLetter` and now are asking how to get `myLetter` from `letter`? – idjaw Oct 08 '16 at 17:32
  • Inside `letter`, I am trying to get `myLetter`. – nedla2004 Oct 08 '16 at 17:35
  • inside `letter`, `myLetter` is named `self`. – Daniel Oct 08 '16 at 17:36
  • @Daniel Oh. That's what the question was? I could not understand the question. – idjaw Oct 08 '16 at 17:37
  • @Daniel Inside `letter`, when I print `self`, I get something like `<__main__.letter object at 0x00000181941AF588>` – nedla2004 Oct 08 '16 at 17:41
  • 1
    Could you explain why you need this? Otherwise this seems like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – OneCricketeer Oct 08 '16 at 17:44
  • @cricket_007 I can't think of when I could **need** this, but I think it could be useful, for example, I could do `c = letter()` instead of `c = letter('c')`. Also if you passed an instance to something, (a function, or another class for example) it could be useful to know the instance's name. (Especially for testing) – nedla2004 Oct 08 '16 at 18:08
  • 2
    An instance doesn't always have a variable name (or only one, for that matter). Consider `list_of_letters = [letter('a'), letter('b')]` or `a = b = letter('c')`. Which variable name would you expect in those cases? What you're asking for is generally not possible (though you *might* be able to get what you want by inspecting the stack), and shouldn't be used even if it was possible. Variable names are not data. If you want a mapping from a "name" string to an instance, use a dictionary. – Blckknght Oct 08 '16 at 18:16
  • 1
    This is nearly impossibel. See http://stackoverflow.com/a/18425275/4273834 – squanto773 Oct 08 '16 at 18:16
  • @Blckknght Those are some great points! When I asked this question I did not think of those cases.Thank you, everyone, for their interest. – nedla2004 Oct 08 '16 at 19:27
  • This is the possible: https://github.com/pwwang/python-varname – Panwen Wang Apr 20 '20 at 20:56
  • @PanwenWang While that is an interesting project, in general this is a terrible idea and I only keep this question up to ward off anyone thinking of doing this just like I wanted to in 2016. – nedla2004 Apr 22 '20 at 15:29
  • @nedla2004 Terrible idea I agree. But also sometimes useful and handy... – Panwen Wang Apr 22 '20 at 15:59

1 Answers1

3

Think of myLetter as a pointer to the class instance of letter. For example, if you have:

LETTERS = list('abcdefghijklmnopqrstuvwxyz')
class letter:
    def __init__(self, name):
        self.number = LETTERS.index(name)
        self.nextLetter = LETTERS[self.number+1]
        self.previousLetter = LETTERS[self.number-1]

myLetter = letter('c')
myLetter2 = myLetter

Both myLetter and myLetter2 are aliases for the exact same myLetter instance. For example:

myLetter.number = 50
print(myLetter2.number)

>>> 50

So finding something to return the name of the letter instance within the letter class is ambiguous.

Brian
  • 1,988
  • 1
  • 14
  • 29