#-*-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就成功不加則報(bào)錯(cuò)#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請(qǐng)問(wèn)這是為什么呢?pyenvversionanaconda-2.0.1(setby/usr/local/opt/pyenv/version)但是放到ideone.com上就又不報(bào)錯(cuò)了
python 中 getattr 使用 為什么這樣報(bào)錯(cuò)?
哆啦的時(shí)光機(jī)
2019-04-21 20:21:27