![](/img/trans.png)
[英]How to put the contents of a (string) variable inside a string using a function - Python
[英]PYTHON Add the contents of a string variable to the beginning of a function?
所以我這里有一個函數,它在一個對象中:
class objectOne:
def funct1(self):
#Do Stuff
當我創建對象的兩個實例時,我想要做的就是這樣做:
dog = objectOne()
cat = objectOne()
當我創建的變量“animal”包含cat時,它將運行:
cat.funct1()
當變量包含dog時,它會運行:
dog.funct1()
有什么辦法嗎? 先謝謝你們。
Ev:D
(澄清一下:我不能在這里使用if語句,因為當我獲得越來越多不同的對象實例時,我不想添加內容,我調用對象函數的最終代碼應該是這樣的:
animal.funct1()
動物是變量,而不是對象的實例。 再次感謝你們!)
使用字典 。 動物將成為鑰匙,物體將成為物品。
例:
animals = {}
animals['cat'] = objectOne()
animal = 'cat'
animals[animal].funct1()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.