免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3287 | 回复: 0
打印 上一主题 下一主题

[转]Python的下划线(_)名字 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-06 22:28 |只看该作者 |倒序浏览

语言学多了太穿越了。所以记录一下Python的下划线命名规则,作为备忘。参
这里

- _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
      import *" does not import objects whose name starts with an underscore.
    - single_trailing_underscore_: used by convention to avoid conflicts with
      Python keyword, e.g.
      Tkinter.Toplevel(master, class_='ClassName')
    - __double_leading_underscore: when naming a class attribute, invokes name
      mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
    - __double_leading_and_trailing_underscore__: "magic" objects or
      attributes that live in user-controlled namespaces.  E.g. __init__,
      __import__ or __file__.  Never invent such names; only use them
      as documented.
大致的意思是:
1. 以单个下滑线开头的名字,在使用from module import *的时候,不会被导入,但是可以用 from module import _obj导入。
2. 以双下滑线开头的名字,是私有类型。也就是不能用obj.__met()这样调用。不过Python实际上并没有私有方法,因为可以这样访问:obj._Obj(类名)__met()。
3. 前后都有双下划线的名字是“Magic”的。这个很多,比如对象初始化方法(构造方法):__init__()等。
4. 以单下划线结尾的名字主要是避免与关键词冲突,这是一种纯粹的习惯用法,并没有语法意义。
转自:http://idevel.cn/blog/articles/name_with_underscore_in_python.html
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103690/showart_2112412.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP