0

I am trying to initialize a class within a function that has the name of the soon to be created object passed into the function like so....

def createPlayer(name):
    name = Player(name1,0)

Where the name variable in createPlayer(name) is the name of the initialized class as seen in name = Player(name1,0)

Currently python does not recognize that the "name" variable being passed into the function is the same as the one creating the instance of the class Player()

Is this possible?

I would like to not have to use a dispatcher as mentioned here.

This is my first post to stackoverflow I don't know all the syntax standards of the community so any pointers would be appreciated.

5
  • Is this something you have done in other languages and are wondering if you can do it in python? How would you use such a class in the rest of your program? It is not clear the usefulness of this. Could you explain further? Commented Jul 6, 2021 at 1:32
  • It's not entirely clear what you want to accomplish, but perhaps read nedbatchelder.com/text/names.html before clarifying your question. Commented Jul 6, 2021 at 1:34
  • The closest I can imagine to what you are trying to do here is you should create a dictionary of all your generated classes and use the name as the key to access the class objects. Commented Jul 6, 2021 at 1:34
  • I am trying to have user input create a new instance of the class named after the name the user inputs, this is most likely not the optimal way to accomplish such task but I was curious if python could do it. Commented Jul 6, 2021 at 2:27
  • my objective is similar to this: stackoverflow.com/questions/15098845/… Commented Jul 6, 2021 at 2:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.