Creating an Instance of a Class with a variable in Python

后端 未结 8 1273
野性不改
野性不改 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条回答
  •  旧时难觅i
    2020-12-29 05:34

    If you just want to pass a class to a function, so that this function can create new instances of that class, just treat the class like any other value you would give as a parameter:

    def printinstance(someclass):
      print someclass()
    

    Result:

    >>> printinstance(list)
    []
    >>> printinstance(dict)
    {}
    

提交回复
热议问题