读书人

python种的属性(很怪异)

发布时间: 2012-10-31 14:37:32 作者: rapoo

python类的属性(很怪异)
前两天写了个python的类,是下面这样的。

class myURL(object):    def setdic(self,value):        self._dic=value    def getdic(self):        return self._dic    dic=property(setdic,getdic)

这个类在我使用的时候,例如对实例的dic属性进行赋值,老报错
url=myURL()url.dic=dic

说setdic需要两个参数,我只给了一个。
TypeError: getdic() takes exactly 1 argument (2 given)
对照着别的正常属性,就是这么用的啊。后来,后来我把最后一句改成这样:
dic=property(getdic,setdic)

程序就正常了,不知道哪位高人能给解释解释。

读书人网 >perl python

热点推荐