2006年04月24日 星期一 17:16
看文档说,能节省内存.但是还是不太清楚具体的机理,以及如何使用. 哪位能解释清楚点. 先谢谢了 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060424/aa656459/attachment.html
2006年04月24日 星期一 17:36
我理解:
就是规定类所拥有的属性。因为python是动态语言,通常情况下,在对象创建之后,也还是
可以向对象添加新的属性。但是这一点不符合的面对对象的含义,有的人不喜欢这样。__slots__就是解决这个问题的。例如:
class test:
__slots__ = ["x","y"]
__init__(self):
self.x =1
self.y =2
对于test这个类就只能有x,y这两个属性了。作为类的使用者,你无法再添加属性了。
在 06-4-24,bird devdoer<devdoer at gmail.com> 写道:
>
> 看文档说,能节省内存.但是还是不太清楚具体的机理,以及如何使用.
> 哪位能解释清楚点.
> 先谢谢了
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
2006年04月24日 星期一 18:47
no.我验证了你的说法不正确 在06-4-24,tocer <tocer.deng at gmail.com> 写道: > > 我理解: > 就是规定类所拥有的属性。因为python是动态语言,通常情况下,在对象创建之后,也还是 > 可以向对象添加新的属性。但是这一点不符合的面对对象的含义,有的人不喜欢这样。__slots__就是解决这个问题的。例如: > class test: > __slots__ = ["x","y"] > __init__(self): > self.x =1 > self.y =2 > 对于test这个类就只能有x,y这两个属性了。作为类的使用者,你无法再添加属性了。 > > 在 06-4-24,bird devdoer<devdoer at gmail.com> 写道: > > > > 看文档说,能节省内存.但是还是不太清楚具体的机理,以及如何使用. > > 哪位能解释清楚点. > > 先谢谢了 > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060424/2b5bd9c9/attachment.html
2006年04月24日 星期一 21:06
恩,写得有问题,下面这个应该没问题了:
class test(object):
__slots__ = ["x","y"]
def __init__(self):
self.x =1
self.y =2
slot = test()
slot.x =10
slot.xx =20 # <----这一句会引发异常
原因是自2.2以后,python有了new-style class ,区别于原先的类,新的类继承自
object,__slots__只对新型的类起作用。
limodou 曾经详细解说过,参看
http://www.donews.net/limodou/archive/2004/12/31/222240.aspx
在 06-4-24,bird devdoer<devdoer at gmail.com> 写道:
> no.我验证了你的说法不正确
>
>
> 在06-4-24,tocer <tocer.deng at gmail.com> 写道:
> > 我理解:
> > 就是规定类所拥有的属性。因为python是动态语言,通常情况下,在对象创建之后,也还是
> >
> 可以向对象添加新的属性。但是这一点不符合的面对对象的含义,有的人不喜欢这样。__slots__就是解决这个问题的。例如:
> > class test:
> > __slots__ = ["x","y"]
> > __init__(self):
> > self.x =1
> > self.y =2
> > 对于test这个类就只能有x,y这两个属性了。作为类的使用者,你无法再添加属性了。
> >
> > 在 06-4-24,bird devdoer< devdoer at gmail.com> 写道:
> > >
> > > 看文档说,能节省内存.但是还是不太清楚具体的机理,以及如何使用.
> > > 哪位能解释清楚点.
> > > 先谢谢了
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
2006年04月25日 星期二 11:36
Thanks. 在06-4-24,tocer <tocer.deng at gmail.com> 写道: > > 恩,写得有问题,下面这个应该没问题了: > class test(object): > __slots__ = ["x","y"] > def __init__(self): > self.x =1 > self.y =2 > > slot = test() > slot.x =10 > slot.xx =20 # <----这一句会引发异常 > > 原因是自2.2以后,python有了new-style class ,区别于原先的类,新的类继承自 > object,__slots__只对新型的类起作用。 > limodou 曾经详细解说过,参看 > http://www.donews.net/limodou/archive/2004/12/31/222240.aspx > > > 在 06-4-24,bird devdoer<devdoer at gmail.com> 写道: > > no.我验证了你的说法不正确 > > > > > > 在06-4-24,tocer <tocer.deng at gmail.com> 写道: > > > 我理解: > > > 就是规定类所拥有的属性。因为python是动态语言,通常情况下,在对象创建之后,也还是 > > > > > 可以向对象添加新的属性。但是这一点不符合的面对对象的含义,有的人不喜欢这样。__slots__就是解决这个问题的。例如: > > > class test: > > > __slots__ = ["x","y"] > > > __init__(self): > > > self.x =1 > > > self.y =2 > > > 对于test这个类就只能有x,y这两个属性了。作为类的使用者,你无法再添加属性了。 > > > > > > 在 06-4-24,bird devdoer< devdoer at gmail.com> 写道: > > > > > > > > 看文档说,能节省内存.但是还是不太清楚具体的机理,以及如何使用. > > > > 哪位能解释清楚点. > > > > 先谢谢了 > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to > > > > python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > Detail Info: > > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060425/18775d08/attachment.htm
Zeuux © 2025
京ICP备05028076号