Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798398
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-08-07 10:47:26

5.9. Private Functions

私有函数

Like most languages, Python has the concept of private elements:

同大多数语言一样,Python同样有私有元素的概念

  • Private functions, which can't be called from outside their module
  • Private class methods, which can't be called from outside their class
  • Private attributes, which can't be accessed from outside their class.
  • 私有函数 不能够从它们所在的类模块的外部调用
  • 私有类方法 不能从他们所在的类类外部调用
  • 私有属性  不能从它们所在的类外部访问

Unlike in most languages, whether a Python function, method, or attribute is private or public is determined entirely by its name.

同大多数语言不同的是,一个Python函数,方法,属性是否私有还是公共是完全由它的名字决定的。

If the name of a Python function, class method, or attribute starts with (but doesn't end with) two underscores, it's private; everything else is public. Python has no concept ofprotected class methods (accessible only in their own class and descendant classes). Class methods are either private (accessible only in their own class) or public (accessible from anywhere).

如果一个Python寒素,类方法,或是属性的名字以两个连续的下划线开始(不是以下划线结束),那它是私有的,剩余所有其它的都是共有的。Python没有受保护的类方法的概念(只有它们后代类和子类可以访问)。

In MP3FileInfo, there are two methods: __parse and __setitem__. As you have already discussed, __setitem__ is a special method; normally, you would call it indirectly by using the dictionary syntax on a class instance, but it is public, and you could call it directly (even from outside the fileinfo module) if you had a really good reason. However,__parse is private, because it has two underscores at the beginning of its name.

MP3Fileinfo中,有两个方法__parse__setiem__中。正如刚才所讨论的,__setitem__是一个特殊方法;通常,在一个类实例中通过使用字典语法,你可能会间接的调用,但是该方法是公共的,你可以直接调用(甚至从fileinfo模块中可以调用),如果你有一个好的理由。然而,__parse是私有的,这是因为在名字前面具有两个下划线。

Note

 

In Python, all special methods (like __setitem__) and built-in attributes (like __doc__) follow a standard naming convention: they both start with and end with two underscores. Don't name your own methods and attributes this way, because it will only confuse you (and others) later.

Python中,所有的特殊方法(如_setitem_)以及内置属性

 

Example 5.19. Trying to Call a Private Method

5.19 尝试调用私有方法

  1. >>> import fileinfo
  2. >>> m = fileinfo.MP3FileInfo()
  3. >>> m.__parse("/music/_singles/kairo.mp3")
  4. Traceback (innermost last):
  5.   File "", line 1, in ?
  6. AttributeError: 'MP3FileInfo' instance has no attribute '__parse'

1

If you try to call a private method, Python will raise a slightly misleading exception, saying that the method does not exist. Of course it does exist, but it's private, so it's not accessible outside the class.Strictly speaking, private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names. You can access the __parse method of the MP3FileInfo class by the name _MP3FileInfo__parse. Acknowledge that this is interesting, but promise to never, ever do it in real code. Private methods are private for a reason, but like many other things in Python, their privateness is ultimately a matter of convention, not force.

如果你尝试调用一个私有方法,Python将会抛出一个轻微的误导性的异常,异常为方法跟不不存在。

当然它是存在的,但是它是私有的,因此它不能从类的外部访问。严格的讲,从类外部是可以访问私有方法,而不是简单的可以访问。在Python没有对象是真正私有的;从内部而言,私有方法以及属性的名字在忙碌的时候能被毁坏以及修复,从而使得他们看起来不能从外部被访问。你可以使用MP3Fileinfo类的_parse方法通过_MP3FileInfo。这是非常有趣的,但是请你记住请不要在在实际的代码中这么干。私有代码的使用有原因的,但是同Python中的许多事情一样,它们的私有是一个惯例问题,不是强制的。

 

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