How do I use the Object's name

Hi all,
So I was away form coding for quite some time, and now that I’m back i’ve tried to
continue from OOP. A question I came onto was how do I use the name that i have given the object without actually giving it the name. For example I could “give it the name”:

def __init__(self, name, abb, belong, abb2):
followed by:
Karachi = city("Karachi", "KHI", "Sindh", "SDH")
But i dont want to do that. Is there a way to be like… I’ll paste the whole code and you’ll know what i mean:

class city():
    def __init__(self, abb, belong, abb2):
        self.abb = abb
        self.belong = belong
        self.abb2 = abb2 
        print(f"The city {i want the name of the object to come here} is abbreviated as {abb} and is located in the province of {belong} which is abbeviated as {abb2}")

Lahore = city("LHE", "Punjab", "PJB")
Karachi = city("KHI", "Sindh", "SDH")
Quetta = city("QTA", "Khyber", "KP")
Peshawar = city('PSH', 'Balochestan','BLT') 

hi
maybe you can try to name the city’s in the “class city” by indenting them. And name the city’s before the def. hope that will help :grinning:

could you give an example?

Feel free to use the same code

No, that’s not possible (without resorting to terrible hacks, that is). If you want to use the name then it must be a string. Variable names are not strings, they’re fundamentally different from any kind of data.

What reasons are there for not giving the class a name field? After all, every city has one and it’s one of its most important properties.

1 Like

sorry i did not know :grimacing:

As @florian said you would need some monster hacks to pull this off which would end up being way more complicated than just passing in the name as a string. But, let me explain why it requires hacks:

  1. You assign Lahore to city(“LHE”, “Punjab”, “PJB”)
  2. This calls the city.__init__ function.
  3. Now, imagined you’re right at the first line of that function: self.abb. Ask yourself, how does that line get access to your variable Lahore?

See how “self.abb” is deep inside class city, and then __init__? That puts it in a different context, and you’d need to be able to somehow “reach” outside of that, and then get at that variable name Lahore. But, it doesn’t stop with that.

When you read this line you read it backwards from what the computer does. Let’s read it:

Lahore = city("LHE", "Punjab", "PJB")

Humans read that as:

“Lahore equals city parens double quote LHE double quote comma Punjab double quote comma double quote PJB double quote parens”

BUT, Python process this basically backwards:

Lahore = city("LHE", "Punjab", "PJB")

Is actually processed:

  1. Create “LHE” string
  2. Create “Punjab” string
  3. Create “PJB” string
  4. Create tuple of 1-3.
  5. Find city.__init__
  6. Call city.__init__ with tuple from 4.
  7. Take result from city.__init__ and THEN create Lahore variable.
  8. Assign Lahore to city.__init__ results.

If you follow these steps (which are approximate, not exactly what Python does) then you can see that the variable Lahore doesn’t even exist until after __init__ is all done.

Finally, there’s this kind of odd compulsion in programming where you’ll want to try to code away repetition. You’ll say, “I keep typing Lahore twice so why not make the 2nd one go away by getting the variable name?”

Then you go down that path and realize that the amount of code and the complexity involved in this is far beyond just doing:

  self.name = name

So you’re not actually improving anything. At that point, repetition is far superior to trying to remove it since the more direct and simple self.name = name will always perform better, have fewer bugs, and be easier to change and understand later.

2 Likes