Chinaunix首页 | 论坛 | 博客
  • 博客访问: 126374
  • 博文数量: 67
  • 博客积分: 1510
  • 博客等级: 上尉
  • 技术积分: 680
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-29 11:22
文章分类

全部博文(67)

文章存档

2011年(10)

2010年(28)

2009年(29)

我的朋友
最近访客

分类: Python/Ruby

2009-12-06 22:28:27

语言学多了太穿越了。所以记录一下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

阅读(646) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~