PYTHON 将字符串变量的内容添加到函数的开头?
所以我这里有一个函数,它位于一个对象中:
class objectOne:
def funct1(self):
#Do Stuff
我想做的是拥有它,所以当我创建该对象的两个实例时:
dog = objectOne()
cat = objectOne()
当我创建的变量“animal”包含 cat 时,它会run:
cat.funct1()
当变量包含dog时,它将运行:
dog.funct1()
有什么方法可以做到这一点吗?预先感谢各位。
Ev :D
(澄清一下:我不能在这里使用 if 语句,因为随着我获得越来越多的对象不同实例,我不想添加东西,我调用对象函数的最终代码应该是比如:
animal.funct1()
其中的动物是变量,而不是对象的实例,再次感谢大家!)
So what I have here is one function, which is in an object:
class objectOne:
def funct1(self):
#Do Stuff
And what I would like to do is have it so when I create two instaces of the object:
dog = objectOne()
cat = objectOne()
When the variable "animal" i have created contains cat, then it would run:
cat.funct1()
And when the variable contains dog, then it would run:
dog.funct1()
Any way to do this? Thanks in advance guys.
Ev :D
(To clarify: I can't use an if statement here because as I gain more and more different instances of the object, I don't want to have to add stuff, my final code to call the object's function should be something like:
animal.funct1()
In which animal is the variable, not an instance of the object. Thanks once again guys!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用字典。动物将是钥匙,物体将是物品。
例子:
Use dictionary. Animals will be keys, objects will be items.
Example: