#-*-coding:utf-8-*-fromsysimportexitclassnewgame(object):def__init__(self,start):self.start=start#self.cccl()defplay(self):next=self.startwhileTrue:print"\n--------"printnextroom=getattr(self,next)next=room()defdeath(self):print"thisisdeath"exit(-1)defcccl(self):print"thisisbbbb"#returnself.al()如果加上return或者exit就成功不加則報錯#exit(1)defal(self):print"thisisal"action=raw_input(">")ifaction=='1':return"death"elifaction=='2':return"cccl"ngame=newgame("al")ngame.play()line17,inplayroom=getattr(self,next)TypeError:getattr():attributenamemustbestring請問這是為什么呢?pyenvversionanaconda-2.0.1(setby/usr/local/opt/pyenv/version)但是放到ideone.com上就又不報錯了
python 中 getattr 使用 為什么這樣報錯?
慕勒3428872
2019-04-10 20:47:47