-2

today I am having an issue with using the value of a variable for making the variable name of a class, so say if we do

class human():
    def __init__ (self, name="unknown", age="unknown")
    self.name=name
    self.age=age

def sethuman():
    x = input("Enter Name: ")
    x = human()

BUT this overwrites the VARIABLE x and sets x to be an object (which will have attributes, etc) when i want the input FROM x (the name entered) to be said object, so instead of using say, "x.name", I wanna say (if the inputted name is John) then "John.name"

Another issue is also if I have a class and there is an object from this class (say John = human("John",27) then how could I later use this in code, as I have no idea how to get this name back later short of putting all of the objects' names into an array and letting the user type which user they wanna edit or look at the attributes of

So if:

def humanhasbeenset():
    John=human("John",27)

def printhuman():
#             ↓Should be "John"
    print(<object name>.name)

Then how would I get the object's name? or a list of object names for that class?

ZoomGC
  • 1
  • 1
  • 2
    See https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables. **TL;DR**: don't, use a dictionary. – jonrsharpe Dec 07 '21 at 22:23
  • I didn't really intend on using dictionaries, I just feel like there's an easier way to use the value of the variable like this – ZoomGC Dec 07 '21 at 22:32
  • So if the user typed in "human" as their name, you're perfectly OK with the resulting object overwriting the class itself, completely destroying the functionality of your program? – jasonharper Dec 07 '21 at 22:34
  • Oh boy... I hadn't thought of that, thanks for bringing that issue to my attention, I could make it so if they try to use human it won't work, so, excluding that issue, how could I get the value of x (John), could getattr be used? Or something similar... I'm vastly confused by this issue and its solutions, I just want a variable name system that a user can use (say, for a game of some kind, to set their character's name, or even use pre-set names) – ZoomGC Dec 07 '21 at 22:40
  • Apologies, jonrsharpe, I didn’t see the comma, I’ll try dictionaries :D – ZoomGC Dec 07 '21 at 23:26
  • 1
    Understand that variable names in code are for YOU, the developer, not for the user. If your user needs to be able to set or view something that thing needs to be the VALUE of a variable, not the name of one. Dictionaries are 100% the answer here. – rdowell Dec 07 '21 at 23:30
  • To clarify, Dictionaries are ABSOLOUTELY the answer, coming back to this nearly a year later makes me facepalm, sorry for the trouble I caused :/ – ZoomGC Sep 24 '22 at 21:05

1 Answers1

1

You would need to inject the user's input into local variable space. From the answer of this post, it is likely impossible to do. Even if you could, Python's offical documentation for exec() recommends not attempting to.

The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function exec() returns.

In this Python bug post, according to George Brandl,

To modify the locals of a function on the fly is not possible without several consequences: normally, function locals are not stored in a dictionary, but an array, whose indices are determined at compile time from the known locales. This collides at least with new locals added by exec. The old exec statement circumvented this, because the compiler knew that if an exec without globals/locals args occurred in a function, that namespace would be "unoptimized", i.e. not using the locals array. Since exec() is now a normal function, the compiler does not know what "exec" may be bound to, and therefore can not treat is specially.

Even if you could do all of this, it still would make for a ton of head aches and messy code, as jasonharper and commenters at this post have noted. An alternative solution, such as storing Human objects in a dictionary, would be better. Without seeing the rest of your code, a quick example would be

humans = {}

class Human:
    def __init__ (self, name="unknown", age="unknown"):
        self.name=name
        self.age=age

def sethuman():
    x = input("Enter Name: ")
    humans[x] = Human(name=x)

def gethuman():
    name = input("Enter name: ")
    if name in humans:
        user = humans[name]
        return user
    else:
        print(f"We could not find {name}."
  • OH MY GOODNESS, I didn’t see jonrsharpe’s comma, my bad! Sorry about that jonrsharpe, I’ll try dictionaries :D – ZoomGC Dec 07 '21 at 23:25