2004年08月09日 星期一 17:11
在手册中有这样一段: 9.6 私有变量 Python对类的私有成员提供了有限的支持。任何形如__spam (以至少双下划线开头,至多单下划线结尾)随即都被替代为 _classname__spam,去掉前导下划线的classname 即当前的类 名。这种混淆不关心标识符的语法位置,所以可用来定义私有类 实例和类变量、方法,以及全局变量,甚至于将其它类的实例保 存为私有变量。混淆名长度超过255个字符的时候可能会发生截 断。在类的外部,或类名只包含下划线时,不会发生截断。 我看得迷糊了。什么叫做“有限的支持”呢?我应该怎样在一个 类里定义一个私有变量呢?是不是按照上面说得以两个下划线开 头? --- limodou <chatme at 263.net> 的正文: > mike gaul,您好! > > > 一个下划线一般是私有变量(只是一个约定),两个下划线一般表示特殊含义。在python编程规范中有介绍。PEP > 8。在http://blog.csdn.net/dreamingk/archive/2004/07/26/51667.aspx有中文的链接。 > > ======= 2004-08-09 15:46:50 您在来信中写道:======= > > >那么以一个下划线和两个下划线开头的有什么区别? > >我看为什么有以两个下划线开头的呢? > >有的前后都有两个下划线。 > > > >不好意思。基础问题。 > > > >_________________________________________________________ > >Do You Yahoo!? > >150万曲MP3疯狂搜,带您闯入音乐殿堂 > >http://music.yisou.com/ > >美女明星应有尽有,搜遍美图、艳图和酷图 > >http://image.yisou.com > >1G就是1000兆,雅虎电邮自助扩容! > >http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > >_______________________________________________ > >python-chinese list > >python-chinese at lists.python.cn > >http://python.cn/mailman/listinfo/python-chinese > > > > = = = = = = = = = = = = = = = = = = = = > > > 致 > 礼! > > > limodou > chatme at 263.net > 2004-08-09 > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > _________________________________________________________ Do You Yahoo!? 150万曲MP3疯狂搜,带您闯入音乐殿堂 http://music.yisou.com/ 美女明星应有尽有,搜遍美图、艳图和酷图 http://image.yisou.com 1G就是1000兆,雅虎电邮自助扩容! http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
2004年08月09日 星期一 17:49
看看下面的例子: >>> class A: ... def __init__(self): ... self.__a = 1 ... >>> a = A() >>> a.__a Traceback (most recent call last): File "", line 1, in ? AttributeError: A instance has no attribute '__a' 这叫做支持 >>> a._A__a 1 >>> 这叫做其实只是有限的支持. _A__a就是所谓混淆名 ----- Original Message ----- From: "mike gaul" <zopemaillistcn at yahoo.com.cn> To: <python-chinese at lists.python.cn> Sent: Monday, August 09, 2004 5:11 PM Subject: Re: [python-chinese] 是不是以下划线开头的都是私有变量/函数 > 在手册中有这样一段: > 9.6 私有变量 > > Python对类的私有成员提供了有限的支持。任何形如__spam > (以至少双下划线开头,至多单下划线结尾)随即都被替代为 > > _classname__spam,去掉前导下划线的classname 即当前的类 > 名。这种混淆不关心标识符的语法位置,所以可用来定义私有类 > 实例和类变量、方法,以及全局变量,甚至于将其它类的实例保 > 存为私有变量。混淆名长度超过255个字符的时候可能会发生截 > 断。在类的外部,或类名只包含下划线时,不会发生截断。 > > 我看得迷糊了。什么叫做“有限的支持”呢?我应该怎样在一个 > 类里定义一个私有变量呢?是不是按照上面说得以两个下划线开 > 头? > > --- limodou <chatme at 263.net> 的正文: > > mike gaul,您好! > > > > > > > 一个下划线一般是私有变量(只是一个约定),两个下划线一般表示特殊含义。在python编程规范中有介绍。PEP > > > 8。在http://blog.csdn.net/dreamingk/archive/2004/07/26/51667.aspx有中文的链接。 > > > > ======= 2004-08-09 15:46:50 您在来信中写道:======= > > > > >那么以一个下划线和两个下划线开头的有什么区别? > > >我看为什么有以两个下划线开头的呢? > > >有的前后都有两个下划线。 > > > > > >不好意思。基础问题。 > > > > > > >_________________________________________________________ > > >Do You Yahoo!? > > >150万曲MP3疯狂搜,带您闯入音乐殿堂 > > >http://music.yisou.com/ > > >美女明星应有尽有,搜遍美图、艳图和酷图 > > >http://image.yisou.com > > >1G就是1000兆,雅虎电邮自助扩容! > > > >http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > > >_______________________________________________ > > >python-chinese list > > >python-chinese at lists.python.cn > > >http://python.cn/mailman/listinfo/python-chinese > > > > > > > = = = = = = = = = = = = = = = = = = = = > > > > > > 致 > > 礼! > > > > > > limodou > > chatme at 263.net > > 2004-08-09 > > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > _________________________________________________________ > Do You Yahoo!? > 150万曲MP3疯狂搜,带您闯入音乐殿堂 > http://music.yisou.com/ > 美女明星应有尽有,搜遍美图、艳图和酷图 > http://image.yisou.com > 1G就是1000兆,雅虎电邮自助扩容! > http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > >
2004年08月10日 星期二 09:59
谢谢,这样的例子让我明白。 再次感谢! --- Jacob Fan <jacob at exoweb.net> 的正文: > 看看下面的例子: > >>> class A: > ... def __init__(self): > ... self.__a = 1 > ... > >>> a = A() > >>> a.__a > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: A instance has no attribute '__a' > 这叫做支持 > >>> a._A__a > 1 > >>> > 这叫做其实只是有限的支持. _A__a就是所谓混淆名 > > ----- Original Message ----- > From: "mike gaul" <zopemaillistcn at yahoo.com.cn> > To: <python-chinese at lists.python.cn> > Sent: Monday, August 09, 2004 5:11 PM > Subject: Re: [python-chinese] > 是不是以下划线开头的都是私有变量/函数 > > > > 在手册中有这样一段: > > 9.6 私有变量 > > > > > Python对类的私有成员提供了有限的支持。任何形如__spam > > > > (以至少双下划线开头,至多单下划线结尾)随即都被替代为 > > > > _classname__spam,去掉前导下划线的classname > 即当前的类 > > > 名。这种混淆不关心标识符的语法位置,所以可用来定义私有类 > > > 实例和类变量、方法,以及全局变量,甚至于将其它类的实例保 > > > 存为私有变量。混淆名长度超过255个字符的时候可能会发生截 > > > 断。在类的外部,或类名只包含下划线时,不会发生截断。 > > > > > 我看得迷糊了。什么叫做“有限的支持”呢?我应该怎样在一个 > > > 类里定义一个私有变量呢?是不是按照上面说得以两个下划线开 > > 头? > > > > --- limodou <chatme at 263.net> 的正文: > > > mike gaul,您好! > > > > > > > > > > > > 一个下划线一般是私有变量(只是一个约定),两个下划线一般表示特殊含义。在python编程规范中有介绍。PEP > > > > > > 8。在http://blog.csdn.net/dreamingk/archive/2004/07/26/51667.aspx有中文的链接。 > > > > > > ======= 2004-08-09 15:46:50 > 您在来信中写道:======= > > > > > > >那么以一个下划线和两个下划线开头的有什么区别? > > > >我看为什么有以两个下划线开头的呢? > > > >有的前后都有两个下划线。 > > > > > > > >不好意思。基础问题。 > > > > > > > > > > >_________________________________________________________ > > > >Do You Yahoo!? > > > >150万曲MP3疯狂搜,带您闯入音乐殿堂 > > > >http://music.yisou.com/ > > > >美女明星应有尽有,搜遍美图、艳图和酷图 > > > >http://image.yisou.com > > > >1G就是1000兆,雅虎电邮自助扩容! > > > > > > >http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > > > >_______________________________________________ > > > >python-chinese list > > > >python-chinese at lists.python.cn > > > > >http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > = = = = = = = = = = = = = = = = = = = = > > > > > > > > > 致 > > > 礼! > > > > > > > > > limodou > > > chatme at 263.net > > > 2004-08-09 > > > > > > > > _______________________________________________ > > > python-chinese list > > > python-chinese at lists.python.cn > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > _________________________________________________________ > > Do You Yahoo!? > > 150万曲MP3疯狂搜,带您闯入音乐殿堂 > > http://music.yisou.com/ > > 美女明星应有尽有,搜遍美图、艳图和酷图 > > http://image.yisou.com > > 1G就是1000兆,雅虎电邮自助扩容! > > > http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > _________________________________________________________ Do You Yahoo!? 150万曲MP3疯狂搜,带您闯入音乐殿堂 http://music.yisou.com/ 美女明星应有尽有,搜遍美图、艳图和酷图 http://image.yisou.com 1G就是1000兆,雅虎电邮自助扩容! http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
Zeuux © 2025
京ICP备05028076号