`
cywhoyi
  • 浏览: 412857 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

python下划线命名

 
阅读更多

 

文章摘录自http://www.th7.cn/Program/Python/2011/08/30/41046.shtml

以下分四种情况说明下划线的作用,python对成员域没有严格控制,大部份只是作为命名规范存在,以下英文部份摘自python官方网站

 

_single_leading_underscore: weak "internal use" indicator.  E.g. "from M import *" does not import objects whose name starts with an underscore.

 

_单下划线开头:弱“内部使用”标识,如:”from M import *”,将不导入所有以下划线开头的对象,包括包、模块、成员

 

single_trailing_underscore_: used by convention to avoid conflicts with

      Python keyword, e.g.Tkinter.Toplevel(master, class_='ClassName')

 

单下划线结尾_:只是为了避免与python关键字的命名冲突

 

__double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).

__双下划线开头:模块内的成员,表示私有成员,外部无法直接调用

clip_image002

 

 

__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.

__双下划线开头双下划线结尾__:指那些包含在用户无法控制的命名空间中的魔术对象或属性,如类成员的__name__ __doc____init____import____file__、等。推荐永远不要将这样的命名方式应用于自己的变量或函数。

另外,以上说的大部分都是与模块成员相关的,包和模块的命名规范又有哪些需要注意的呢?

      Package and Module Names               

 

Modules should have short, all-lowercase names.  Underscores can be used

      in the module name if it improves readability.  Python packages should

      also have short, all-lowercase names, although the use of underscores is

      discouraged.     
                Since module names are mapped to file names, and some file systems are

      case insensitive and truncate long names, it is important that module

      names be chosen to be fairly short -- this won't be a problem on Unix,

      but it may be a problem when the code is transported to older Mac or

      Windows versions, or DOS.

 

包和模块:模块应该使用尽可能短的、全小写命名,可以在模块命名时使用下划线以增强可读性。同样包的命名也应该是这样的,虽然其并不鼓励下划线。

以上这些主要是考虑模块名是与文件夹相对应的,因此需要考虑文件系统的一些命名规则的,比如Unix系统对大小写敏感,而过长的文件名会影响其在Windows/Mac/Dos等系统中的正常使用。

    Class Names

 

      Almost without exception, class names use the CapWords convention.

      Classes for internal use have a leading underscore in addition.

类:几乎毫无例外的,类名都使用首字母大写开头(Pascal命名风格)的规范。使用_单下划线开头的类名为内部使用,上面说的from M import *默认不被告导入的情况。

 

    Exception Names

 

      Because exceptions should be classes, the class naming convention

      applies here.  However, you should use the suffix "Error" on your

      exception names (if the exception actually is an error).

异常:因为异常也是一个类,所以遵守类的命名规则。此外,如果异常实际上指代一个错误的话,应该使用“Error”做后缀

    Global Variable Names

 

      (Let's hope that these variables are meant for use inside one module

      only.)  The conventions are about the same as those for functions.

 

      Modules that are designed for use via "from M import *" should use the

      __all__ mechanism to prevent exporting globals, or use the older

      convention of prefixing such globals with an underscore (which you might

      want to do to indicate these globals are "module non-public").

 

 

    Function Names

 

      Function names should be lowercase, with words separated by underscores

      as necessary to improve readability.

 

      mixedCase is allowed only in contexts where that's already the

      prevailing style (e.g. threading.py), to retain backwards compatibility.

函数:小写、下划线分词,如def has_key(ch):

 

    Function and method arguments

 

      Always use 'self' for the first argument to instance methods.

 

      Always use 'cls' for the first argument to class methods.

 

      If a function argument's name clashes with a reserved keyword, it is

      generally better to append a single trailing underscore rather than use

      an abbreviation or spelling corruption.  Thus "print_" is better than

      "prnt".  (Perhaps better is to avoid such clashes by using a synonym.)

 

    Method Names and Instance Variables

 

      Use the function naming rules: lowercase with words separated by

      underscores as necessary to improve readability.

 

      Use one leading underscore only for non-public methods and instance

      variables.

 

      To avoid name clashes with subclasses, use two leading underscores to

      invoke Python's name mangling rules.

 

      Python mangles these names with the class name: if class Foo has an

      attribute named __a, it cannot be accessed by Foo.__a.  (An insistent

      user could still gain access by calling Foo._Foo__a.)  Generally, double

      leading underscores should be used only to avoid name conflicts with

      attributes in classes designed to be subclassed.

 

      Note: there is some controversy about the use of __names (see below).

 

    Constants

 

       Constants are usually defined on a module level and written in all

       capital letters with underscores separating words.  Examples include

       MAX_OVERFLOW and TOTAL.

 

分享到:
评论

相关推荐

    Python中下划线的使用方法

    主要介绍了Python中下划线的使用方法,是为python编程学习中的基本知识,需要的朋友可以参考下

    驼峰命名转下划线命名

    场景:json数据常以驼峰命名,需要转下划线命名,以对应Python或数据库字段名 命名方式 说明 特点 适用领域 示例 下划线命名 单词间用下划线分隔 清晰 Python、MySQL、Oracle teacher_name 驼峰命名 第一个...

    python的命名规则知识点总结

    python命名规则 命名风格 python几种不同命名风格 驼峰式命名法(WjW) 混合式命名法(wjWj) 大写(WJWJWJ)或大写加下划线(WJWJWJ) 前缀(wjing)或后缀(ingwj)下划线,有时双下划线 变量 python变量分为: (1)常量 ...

    一文轻松掌握python语言命名规则(规范)

    和C/C++、Java等语言一样,python在命名上也有一套约定俗成的规则,符合规范的命名可以...1.2. python的变量名字中可以包含英文、下划线、数字,但是不能以数字开头。 也就是说,student_id、student1、student_1、stu

    python 变量命名规范1

    2.私有类成员使用单一下划线前缀标识,多定义公开成员,少定义私有成员 3.变量名不应带有类型信息,因为Python是动态类型语言 2.命名中含有长单词,对某个单

    浅谈Python中带_的变量或函数命名

    主要介绍了浅谈Python中带_的变量或函数命名,简单介绍了Python编程风格的描述文档,以及带有下划线的命名规则,具有一定参考价值,需要的朋友可以了解下。

    一文轻松掌握python语言命名规范规则

    和C/C++、Java等语言一样,python在命名上也有一套约定俗成的规则,符合规范的命名可以让程序...1.2. python的变量名字中可以包含英文、下划线、数字,但是不能以数字开头。 也就是说,student_id、student1、student_

    python标识符命名规范原理解析

    这篇文章主要介绍了python标识符命名规范原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简单地理解,标识符就是一个名字,就好像我们每个人都有属于...

    Python程序基础:Python中的变量.pptx

    下划线连接命名 2.1.1 变量的命名 2.1 变 量 提示 (1)Python的变量名区分英文字母大小写,如score和Score是两个不同的变量。 (2)变量名不能是Python的关键字。 >>> import keyword #导入keyword模块 >>> ...

    和孩子一起学习python之变量命名规则

    变量命名规则 下面是关于变量名(也称为标识符)的一些规则 必须以一个字母或一个下划线字符开头。后面可以使用一个字母、数字或下划线字符的序列,长度不限。 字母可以是大写或小写,大小写是不同的。也就是说,...

    详解python里的命名规范

    全小写,可使用下划线 包 应该是简短的、小写的名字。如果下划线可以改善可读性可以加入。如mypackage。 模块 与包的规范同。如mymodule。 类 总是使用首字母大写单词串。如MyClass。内部类可以使用额外的前导下划线...

    浅谈python 里面的单下划线与双下划线的区别

    在学习Python的时候,很多人都不理解为什么在方法(method)前面会加好几个下划线,有时甚至两边都会加,比如像 __this__ 这种。在我看到上面的文章之前,我一直以为Python中这些下划线的作用就像Golang中方法/函数...

    Python 变量类型及命名规则介绍

    首字母为英文和下划线,其它部分则可以是英文、数字和下划线(即:_),而变量名称是区分大小写,即变量temp与Temp为不同变量。变量的基本用法如下: 复制代码 代码如下:# 例:使用变量a = 10b = 20print a + b>>> 30...

    Python编程规范要求

    2. 命名规范:变量、函数和类的命名应具有描述性,使用小写字母和下划线的组合,遵循下划线命名法。 3. 注释:为了增加代码的可读性,应在关键代码块和函数上方添加注释,解释代码的功能和实现思路。 4. 函数和方法...

    python 类的私有变量和私有方法.docx

    在Python中可以通过在属性变量名前加上双下划线定义属性为私有属性 特殊变量命名 1、 _xx 以单下划线开头的表示的是protected类型的变量。即保护类型只能允许其本身与子类进行访问。若内部变量标示,如: 当使用...

    Python学习心得(9)

    必须符合表示符的命名规则,并且符合驼峰结构(第一个单词以小写字母开头,其余单词首字母大写 , 如:theFirstName)或者在单词中间加上下划线(_)#推荐使用下划线 2)参数: 参数分为可变参数和不可变参数,可变...

    【Python入门基础】变量类型及命名(包括实例)

     变量名由字母、数字和下划线构成,数字不能开头;  大小写敏感(大写的‘A’和小写的‘a’是两个不同的变量);  不要跟关键字和系统保留字冲突。 2.PEP 8要求:Python Enhancement Proposal的缩写,通常翻译为...

    python基础试题(含答案)优质.doc

    要利用Python通过数组绘制拟合曲线图,必须要用到的外部库是( ) A.time库 B.random库 C.turtle库 D.matplotlib 库 6.Python中变量的命名遵循的规则,不正确的是 ( ) A.必须以字母或下划线开头,后面可以是...

    Python基础教程第2章.pptx

    Python程序设计教程 授课教师: 职务...Python的标识符命名规则如下: 标识符名字的第1个字符必须是字母或下划线(_); 标识符名字的第1个字符后面可以由字母、下划线(_)或数字(0~9)组成; 标识符名字是区分大小写

    python 命名规范知识点汇总

    1,模块命名 (1)模块推荐使用小写命名, (2)除非有很多字母,尽量不要用下划线 因为很多模块文件存与模块名称一致的类,模块采用小写,类采用首字母大写,这样就能区...(2)类内部变量命名,用单下划线(_)开头

Global site tag (gtag.js) - Google Analytics