在Python中使用变量创建类的实例
我之前发过帖子,但不是很清楚,我在回答问题时遇到了麻烦。自从我编辑它以使其更有意义以来,人们似乎一直没有看它,也许是因为他们看到它已经有了6个答案。所以我把它重新贴在这里: 我对Python和编程还是个新手,所以我需要简单的解释!我甚至不知道你说的字典是什么 我想给我妹妹做一个游戏。这是一种虚拟宠物的东西,宠物有玩具可以玩 我创建了一个类玩具,想要创建一个函数,getNewToyname,data1,data2,data3,data4,data5 我希望这个函数能够创建一个新的类Toy实例,并且每次创建一个新实例时都能够多次调用这个函数 根据我的经验,您创建的实例具有:在Python中使用变量创建类的实例,python,class,Python,Class,我之前发过帖子,但不是很清楚,我在回答问题时遇到了麻烦。自从我编辑它以使其更有意义以来,人们似乎一直没有看它,也许是因为他们看到它已经有了6个答案。所以我把它重新贴在这里: 我对Python和编程还是个新手,所以我需要简单的解释!我甚至不知道你说的字典是什么 我想给我妹妹做一个游戏。这是一种虚拟宠物的东西,宠物有玩具可以玩 我创建了一个类玩具,想要创建一个函数,getNewToyname,data1,data2,data3,data4,data5 我希望这个函数能够创建一个新的类Toy实例,并且
class Toy:
def __init__(self, name, data1, data2, data3, data4, data5):
pass
myToy = Toy(myToy, 1, 2, 3, 4, 5)
然后将类中的方法用于:
myToy.method1()
鉴于我希望能够拥有多个玩具,每个玩具都有一个playWith方法,我希望每次调用一个玩具时,实例都能反映出玩具的名称
class Toy:
def __repr__(self):
return "<type Toy name=%s>" % self.name
我希望每次调用getNewToy方法时实例都不同,。。。以及反映名称的实例
记住,我是编程新手,所以你可以保持简单的解释
非常感谢,现在更容易理解了 创建一个特殊的getNewToy函数没有意义。只需创建类:
newtoy = Toy(name, data1, data2, data3, data4, data5)
这就是你需要做的
方法我希望实例在每次调用玩具时反映玩具的名称
class Toy:
def __repr__(self):
return "<type Toy name=%s>" % self.name
制作一个特殊的getNewToy函数是没有意义的。只需创建类:
newtoy = Toy(name, data1, data2, data3, data4, data5)
这就是你需要做的
方法我希望实例在每次调用玩具时反映玩具的名称
class Toy:
def __repr__(self):
return "<type Toy name=%s>" % self.name
我不明白你为什么需要getNewToy方法。每次调用Toy时,都会得到一个新的类实例。您可能需要这样的代码:
class Toy:
def __init__(self, name, data1, data2, data3, data4, data5):
self.name = name
self.data1 = data1
# ...
myToy = Toy("firsttoy", 1, 2, 3, 4, 5)
myToy2 = Toy("2ndToy", 6, 7, 8, 9, 10)
我不明白你为什么需要getNewToy方法。每次调用Toy时,都会得到一个新的类实例。您可能需要这样的代码:
class Toy:
def __init__(self, name, data1, data2, data3, data4, data5):
self.name = name
self.data1 = data1
# ...
myToy = Toy("firsttoy", 1, 2, 3, 4, 5)
myToy2 = Toy("2ndToy", 6, 7, 8, 9, 10)
下面是我将如何做你解释的事情:
# The following two classes are toys, both have a playWith
# as you wanted, each playWith do different things
class Ball:
def __init__(self):
self.name = "ball"
def playWith(self):
print "the ball bounces"
class Car:
def __init__(self):
self.name = "car"
def playWith(self):
print "the car is fast"
# This is a Python generator, every time .next() is called on it,
# the next "yield-value" is returned
def generator():
while True:
yield Ball()
yield Car()
# This is the creator, it has to be a class rather than a function
# since you wanted a new toy each time getNewToy is called
# and as such the generator needs to be tracked
class ToyCreator:
def __init__(self):
self.generator = generator()
def getNewToy(self):
return self.generator.next()
# Create five toys, print their name and play with them
# Do note here that even though we ask for five toys but only have
# two "yields" in the generator, the generator "wraps around" (since,
# internally, its just an endless loop)
toyCreator = ToyCreator()
for i in range(5):
toy = toyCreator.getNewToy()
print "Toy",i,toy.name,"\t:",
toy.playWith()
如果您在理解收益率业务方面遇到困难,请查看以下内容
更准确地说,你要做的是实现a,a
还感到困惑吗?再读一遍,想一想,但不要犹豫问。我们是来帮忙的 以下是我将如何按照你的解释行事:
# The following two classes are toys, both have a playWith
# as you wanted, each playWith do different things
class Ball:
def __init__(self):
self.name = "ball"
def playWith(self):
print "the ball bounces"
class Car:
def __init__(self):
self.name = "car"
def playWith(self):
print "the car is fast"
# This is a Python generator, every time .next() is called on it,
# the next "yield-value" is returned
def generator():
while True:
yield Ball()
yield Car()
# This is the creator, it has to be a class rather than a function
# since you wanted a new toy each time getNewToy is called
# and as such the generator needs to be tracked
class ToyCreator:
def __init__(self):
self.generator = generator()
def getNewToy(self):
return self.generator.next()
# Create five toys, print their name and play with them
# Do note here that even though we ask for five toys but only have
# two "yields" in the generator, the generator "wraps around" (since,
# internally, its just an endless loop)
toyCreator = ToyCreator()
for i in range(5):
toy = toyCreator.getNewToy()
print "Toy",i,toy.name,"\t:",
toy.playWith()
如果您在理解收益率业务方面遇到困难,请查看以下内容
更准确地说,你要做的是实现a,a
还感到困惑吗?再读一遍,想一想,但不要犹豫问。我们是来帮忙的 > P>你可能总是有5个数据项,但即使如此,也要考虑使用*ARG:
class Toy:
def __init__(self, name, *args):
self.name = name
self.data = args
也许你总是有5个数据项,但即使如此,也要考虑使用*ARG:
class Toy:
def __init__(self, name, *args):
self.name = name
self.data = args
哦,事实上,第二次在第三次左右你会得到相同的答案,因为你的问题没有改变。但是这次人们让不太懂编程的人更容易理解!Jasper,继续检查你之前的问题,寻找新的答案。哦,事实上,第二次你会在第三次左右得到相同的答案,因为你的问题没有改变。但是这一次人们让不太懂编程的人更容易理解!贾斯珀,继续检查你之前的问题,寻找新的答案。