星期三, 六月 20, 2007

python __doc__ and gettext

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import re
from gettext import gettext as _

#1
__doc__ = _("""
Author: Roc Zhou
Date: 2007-06-20
Email: chowroc.z@gmail.com

A new data structrue: Tree
...""")

class Tree:
#2
__doc__ = _("""This class define a builtins like tree type of data structure,
thus now we can manipulate a tree very conveniently, like this:
head = Tree(value)
head = Tree(value, data=value1, extra=value2)
head.branch = value
...""")
......
def __init__(self, value, **kwargs):
_(""""head = Tree(value)" create a Tree instance as the root node with node 'value'.
...""")
......
def Node1(self, pathseq, value):
#3
_("""Construct a Tree node from a path sequence, this path sequence
only represents a single node, for example:
...""")
这里对 #1, #2 都使用 "__doc__ = " 的操作,而不是默认的直接定义。对于#1,可能是因为前面使用了 from gettext import gettext as _,因为需要国际化支持,所以必须先这么做,但导致 __doc__ 默认定义不在 module 顶部,所以可能需要使用 __doc__ 来指定。

而#2可能是因为 gettext 返回的值使用默认定义方法无法正确传递给 __doc__,因为打印的结果为 None。

但是对于#3,无论是否使用 "__doc__ = " 都无效,只能使用默认定义,不知道是什么原因?

没有评论: