Creating an Instance of a Class with a variable in Python

后端 未结 8 1235
野性不改
野性不改 2020-12-29 05:23

I\'m trying to create a game for my little sister. It is a Virtual Pet sort of thing and the Pet has toys to play with.

I created a class Toy and want t

8条回答
  •  孤城傲影
    2020-12-29 05:35

    I think you can use eval. Something like this

    def toclass(strcls):
        return eval(strcls)()
    

提交回复
热议问题