| << 1 ˌ 킯챉 | 3 ˌ ̹皂 >> |
ˌ˼ ˂ һ Ꭻ: ȍ. ̂, ݵ ˼ ̹, ȍ ˅ ݵ ﹚ ̹ , ۼ յ, ס굎 ፫ . , ﹚ ˂졒, в 빚 ﹚ ̉, 皲 ˜ 衒 ˅ ﹚ 禂 .
˟, ˅. ˼ ˧ 킯 ˅ . 뵎˼ 킯챉 ፲ Ҋ 篫. һ һ ۂ ; ˼ ˌ ݵ 굎 Տ .
, ˼ ꡜ ̴ ȍ ϵ ˅.(Windows, UNIX, Mac OS).
def help(object, spacing=10, collapse=1):![]()
![]()
"""Print methods and doc strings. Takes module, class, list, dictionary, or string.""" methodList = [method for method in dir(object) if callable(getattr(object, method))] processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList]) if __name__ == "__main__":
![]()
print help.__doc__
| ݵ˼ ˂ ﹚ 翫, help. ס˂ ﹚ , ס˼ ˂ Ҽ ̑: object, spacing, collapse. ҫ ˞ Ҽ뵴, ס 廡 ȓ . | |
|
| help ﹚ -˂ ը ը (doc string) 皫嵴 ס˼ ﹚˂ ˞ . ՠ ݱ ˸ 즗; ﹚ 즗 ˤ ȍ . |
|
| ﹚ ˂ ӱ ˅. |
|
| "if __name__" Ӏ ˼ ˞ , ˤ ݵ ȍ վ , ռ ˅. , ˼ help﹚˂ doc string ̉傄. |
|
| if йը˼ '==' ˤ ȍ졒, 飫 . |
help ﹚ , ˂ IDE ւ , ȍ . ס˼ ﹚ ˼ (ݵ ˼, ס˼ ﹚ 翫, ˼ ˼, ס˼ 翫) ̹ ̑ ﹚ ˂ doc string ̉傄.
Example 2.2. Sample usage of apihelper.py
>>> from apihelper import help >>> li = [] >>> help(li) append L.append(object) -- append object to end count L.count(value) -> integer -- return number of occurrences of value extend L.extend(list) -- extend list by appending list elements index L.index(value) -> integer -- return index of first occurrence of value insert L.insert(index, object) -- insert object before index pop L.pop([index]) -> item -- remove and return item at index (default last) remove L.remove(value) -- remove first occurrence of value reverse L.reverse() -- reverse *IN PLACE* sort L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1
̉˼ ˅ е. - doc string˼ ˂ ̀翫, һ ȍ˼ collapse 빚 0 ۂ븇 ˅. ﹚˂ 10 ҄, ˼ spacing 빚 ۂ ̉ ˅.
Example 2.3. Advanced usage of apihelper.py
>>> import odbchelper >>> help(odbchelper) buildConnectionString Build a connection string from a dictionary Returns string. >>> help(odbchelper, 30) buildConnectionString Build a connection string from a dictionary Returns string. >>> help(odbchelper, 30, 0) buildConnectionString Build a connection string from a dictionary Returns string.
˼ ﹚ 빚 皵 ; ﹚ 빚 翫, 빚 ˂ ̑. , 빚˼ ˅ 빚 ȍ븇 Ц ۵ ˅. SQL ՚˂ Transact/SQL ˜ˌ 稵˼ ˅; SQL՚˂ Ү ˟ը, ˼ ˌ ǂ ˅.
Example 2.4. help, a function with two optional arguments
def help(object, spacing=10, collapse=1):
spacing collapse ˞, Ҁ ס굎˼ ˂ 皱 ը. object 湚˞, Ҁ ס˼ ȡ 汉 ը. help һ˂ 빚 ̉, spacing ˼ 10 ȡ 졒 collapse 1 ȡ . help ˂ 빚 ̉, collapse ˟ 1 ȡ .
collapse һ˂ ۂ챉 spacing ˤ ˼ . ˂ ݲ, ˼ , Ҁ ˼ ț ˂ 빚 ﹚ ̉ ը. һ Ы, 빚 Ц, ۵ ˅.
Example 2.5. Valid calls of help
help(odbchelper)help(odbchelper, 12)
help(odbchelper, collapse=0)
help(spacing=15, object=odbchelper)
| ˂ 빚, spacing˼ 10 collapse ȡ 1 翫. | |
| ˂ 빚, collapse ȡ 1 翫. | |
| ˼ collapse빚 ˞ 졒 ˂ ۂ. spacing˼ ˟ ˂ ȡ 10 翫. | |
| 皲 湚˞ 빚ֵ (object ˼, ס˼ ) 皲 ˅, ˅ 빚 Ц һ ˅. |
빚 ȍ˟ ˟皫 ˼ ȗ . 빚 ﹚ ̉ “ȗ˞” վ˼ ̅ ˼ ﹚ ۵ 빚˂ ̧ ȫ. ˂ ̡, ˼ “ȗ˞” վ ﹚ ̉ , һ ˼ ˞ ¹ ȗ 翫.
| ﹚ ̉챉 ˤ 삄 ˼ (ȡ е) 빚 ۂ ; վ Ы ˅. | |
ܲ
ˌ ﹚˂ ˼ ˅. ݵ ﹚˼ 僡Ҳ稹 ݵ «. ˼ ˂˞ 嵴, ݲ˂ (, 煲 ֡ ˼) Ү ݲ 남皫 ᡒ .
type ﹚ ݵ ˂˞ ̹˂ ՠ. ˼ types ݵ һе. ˼ (Ϯ) ﹚ ˂ ˅.
>>> type(1)<type 'int'> >>> li = [] >>> type(li)
<type 'list'> >>> import odbchelper >>> type(odbchelper)
<type 'module'> >>> import types
>>> type(odbchelper) == types.ModuleType 1
str ˼ ը ́ӫ. ݵ ˼ ը ϼ ˅.
>>> str(1)'1' >>> horsemen = ['war', 'pestilence', 'famine'] >>> horsemen.append('Powerbuilder') >>> str(horsemen)
"['war', 'pestilence', 'famine', 'Powerbuilder']" >>> str(odbchelper)
"<module 'odbchelper' from 'c:\\docbook\\dip\\py\\odbchelper.py'>" >>> str(None)
'None'
| ۹ ˼ , ˼ str ȗ , Ҁ ˂ ݵ ݲ˼ ۹ ը 煫 ﹚ 皡 ˅ ը. | |
| , str ˼ ˂ . ס˼ 禡禡 ҂ . | |
| str ˼ ݵ . ˼ ݵ˂ ը ݸ˂ ݵ˂ ւ졒, ˂ ˼ . | |
| str˂ ς ˤ ס ˂ null , None . ס˼ ը 'None' ՠ. help﹚ , ס 廡 ȓ . |
˂ help ﹚˂ ˌ 傄 dir﹚ ˅. dir ﹚ ̹ ˂ ՠ: ݵ(modules), ﹚(functions), ը(strings), (lists), ȍ˟(dictionaries)... ˼ .
>>> li = [] >>> dir(li)['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> d = {} >>> dir(d)
['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values'] >>> import odbchelper >>> dir(odbchelper)
['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']
| li , dir(li) ˂ ݵ ՠ. ˼ ՠ ˂ , ̹ 즗, ը ւ . | |
| d ȍ˟, dir(d) ˼ ȍ˟ ˂ ՠ. ˞ 굎 һ, (keys), ւ . | |
| ˍ˅ 皫 . odbchelper ݵ, dir(odbchelper) , ˌ ւ, __name__ __doc__, ˂ ݵ ˼, ݵ ˂ ݵ ˂ 굎 һ ՠ. , odbchelper һ˂ ȍ-˂ , buildConnectionString ﹚ 皫嵴 킯챉˂ ˌ 낄 ˅. |
, ̉ ﹚ ̹ ̑ ̹ ̉, 1 ՠ졒 , 0 ՠ. ̉ ̹ ﹚(functions), (class) (methods), 皲 皵 ւ. ( 3 ˌ Ꭻ.)
Example 2.9. Introducing callable
>>> import string >>> string.punctuation'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>> string.join
<function join at 00C55A7C> >>> callable(string.punctuation)
0 >>> callable(string.join)
1 >>> print string.join.__doc__
join(list [,sep]) -> string Return a string composed of the words in list, with intervening occurrences of sep. The default separator is a single space. (joinfields and join are synonymous)
| string ݵ ˅ ﹚˼ (˼ ȍֵ ˟ join ﹚ ȍ 졒), һ ݵ˼ string.punctuation ˼ ˼ ﹚ ւ졒 ˅, ﹚ ݵ ˞ ը ւ. | |
| string.join ը˂ ¡ 煫 ﹚. | |
| string.punctuation ˼ ̉ ; ס˼ ը. (ը˼ ̉ 皡 ˅, һ ը ̹ ̉ .) | |
| string.join˼ ̉; ﹚ ˂ 빚 皫 ﹚. | |
| ̉ ̹ doc string ˅. ̉ (callable) ﹚ ̹˂ ȍ, 범 (methods, functions, classes) Ǹ̂챉 (constants, etc.) , ̹ 굵 ȍ˟ е, ۂ ˅ . |
type, str, dir, ˂ ݵ һ ˌ ﹚˼ 皲 '__builtins__' ݵ «. (ס˼ ˂ կ ͡ 翫.) , ˼ ˸ ˞ 'from __builtins__ import *' ࡢ ˅, ס˼ ݵ “ˌ(built-in)” ﹚ 졒 ˼ ס굎 ˞ ȍ ˅.
վ ࡢ ˼ __builtins__ ݵ ۼ յ븇 ݵ ˌ ﹚ һ˂ 碱 ˅ . 貮̧ , ס ˤ һ˂ ﹚ 皡 ˅; ס˼ help . ̵ 皱 f ; ﹚ . (AttributeError ˼, ˌ ˼ ւ .)
Example 2.10. Built-in attributes and functions
>>> from apihelper import help >>> help(__builtins__, 20) ArithmeticError Base class for arithmetic errors. AssertionError Assertion failed. AttributeError Attribute not found. EOFError Read beyond end of file. EnvironmentError Base class for I/O related errors. Exception Common base class for all exceptions. FloatingPointError Floating point operation failed. IOError I/O operation failed. [...snip...]
| ˫嵴, ס ˼ ˟ ۵ ϡ ݵ ݵ Տ . һ ˂ ݲ ˼ ݵ ȍ ˤ (˼ , ˼, ȗһ, MSDN) 禂졒 ˅ է, ˼ ̹-ը(self-documenting) ˅. | |
ܲ
˼ ﹚ ̹ . ˼ getattr ﹚ ȍ, ̱ е ﹚˂ յ ˅ .
Example 2.11. Introducing getattr
>>> li = ["Larry", "Curly"] >>> li.pop<built-in method pop of list object at 010DF884> >>> getattr(li, "pop")
<built-in method pop of list object at 010DF884> >>> getattr(li, "append")("Moe")
>>> li ["Larry", "Curly", "Moe"] >>> getattr({}, "clear")
<built-in method clear of dictionary object at 00F113D4> >>> getattr((), "pop")
Traceback (innermost last): File "<interactive input>", line 1, in ? AttributeError: 'tuple' object has no attribute 'pop'
| ˼ ˂ pop յ. ˼ pop ̉ 즗 즗; ̉˼ li.pop() . ˼ . | |
| pop ՠ, һ , getattr ﹚ ը 빚 ۵. getattr˼ Ҕ ۵ ˌ ﹚ ̹˂ ՠ. , ̹ , ˼ pop . | |
| һ 皡 ˧ , ̵ : getattr˂ ՠ ˼ , ˼ ס li.append("Moe") ˞ Ȃ ꡜ ̉ ˅. һ ˼ ﹚ ˞ ̉ ; ˼ ﹚˂ ը ۂ. | |
| getattr˼ ȍ˟ . | |
| ˞, , getattr˼ , getattr˼ 皦 է́ . |
getattr ˌ ˤ ˼ . ס˼ ݵ .
Example 2.12. getattr in apihelper.py
>>> import odbchelper >>> odbchelper.buildConnectionString<function buildConnectionString at 00D18DD4> >>> getattr(odbchelper, "buildConnectionString")
<function buildConnectionString at 00D18DD4> >>> object = odbchelper >>> method = "buildConnectionString" >>> getattr(object, method)
<function buildConnectionString at 00D18DD4> >>> type(getattr(object, method))
<type 'function'> >>> import types >>> type(getattr(object, method)) == types.FunctionType 1 >>> callable(getattr(object, method))
1
| ˼ odbchelper ݵ ˅ buildConnectionString ﹚ ՠ, ס 킯챉 낧. ( 16 煹 ȁ ۂ ; ˂ ̉塇 .) | |
| getattr ȍ, ˼ ﹚ ˼ յ ˅. ՠ˞, getattr(object, "attribute") object.attribute . ̹(object) ݵ, (attribute)˼ ݵ ˂ ˅: ﹚(a function), (class), ˼ ˟ (global variable). | |
| help﹚ ȍ . ̹(object) 빚 ﹚ ҄翫; (method) ը ˼ ﹚˂ . | |
| , method ﹚˂ , ס ס˂ (type) յ븇 ˅. | |
| (method) ﹚Ղ, ס˼ ̉. |
̂, ˼, տ , ߫ 傄 皡 ˅. ˼ ހǫ瘡 ͵ ˅嵴, ˅ ˼ 皲 皡 굎˼ ˟ ǵ翫.
Example 2.13. List filtering syntax
[mapping-expression for element in source-list if filter-expression]
˼ 衒 ˅ 粮 տ˂ ˌ տ 3˂ 2 ; if , һ ˼ ȱ . ˼ ˼ ˅ (ס˼ ˂ ݵ ). ߱ ւ﵃ . ݵ ˼ Ǹ̵, ס굎˼ ߱ 埡 ̉ ւ﵂ .
Example 2.14. Introducing list filtering
>>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"] >>> [elem for elem in li if len(elem) > 1]['mpilgrim', 'foo'] >>> [elem for elem in li if elem != "b"]
['a', 'mpilgrim', 'foo', 'c', 'd', 'd'] >>> [elem for elem in li if li.count(elem) == 1]
['a', 'mpilgrim', 'foo', 'c']
| ߱ ˼ (ס˼ ˂ ՠ), 秂. ˼ , ; , 皲稹 ߱ ˂ ՠ ւ. ˼ ݵ -ը ը ȡ ˅ , ס ը˂ ӡ翫. | |
| ˼ , b ȫ . ˼ b˂ ݵ ̉ ȫ 뵴, Ҁ ס ̉ , ˼ ը . | |
| count һ˂ һһ 鸹 ՠ. ˂ һ˂ ȍ ւ ՠ, ˼ һ˂ 秼 ϡ ࡢ 皵 . һ , Ҁ ˂ տ һһ ˼ ( , b d) ˟ ը. 秼 ϡ վൎ ˅ , һ ̴˼ . |
Example 2.15. Filtering a list in apihelper.py
methodList = [method for method in dir(object) if callable(getattr(object, method))]
˼ ˉ , ˼ ˉ, һ 禫 . ˟̹ ˼ һ˂ ՠ졒, methodList . ˂ ˟ՠ ߱ . ߱ ˼ ; ס˼ ˂ ՠ. dir(object) ̹(object)˂ ˂ ՠ; ߡ ˅ . if˂ ț .
˼ ᮡ , һ . ˼ callable, getattr, in . ˟ , object ݵ e ݵ ˅ ﹚˂ , 'getattr(object, method)'˼ ﹚̹ ՠ.
˼ object 皲, һ˂ ̹ ̑, ̹˂ , , ﹚ ݵ 굎˂ յ, Ȳ ݵ 굎 ϡ. , getattr﹚ , //﹚˂ ̑ 굎 յ븇 ϡ . ̹ ̉ 肄, ס˼ ﹚, (˂ pop ˼) ˌ ﹚ (odbchelper ݵ˂ buildConnectionString﹚ ˼) ȍ-˂﹚ ˅. ݵ ݵ ˌ ˅ __name__ ˼, ˼ ȗ .
ܲ
, and or ȗ Ғ , һ ˼ ՠ ; ס굎˼ ҂졒 ˅ ˂ һ ՠ.
>>> 'a' and 'b''b' >>> '' and 'b'
'' >>> 'a' and 'b' and 'c'
'c'
| and ȍ , ˼ ը . '0, '', [], (), {}, None'˼ ݵ ը ; ݵ ˼ .[3] ݵ ը , and ՠ. , and 'a' , 졒 'b' , 'b' ՠ. | |
| ը , and տ ՠ. , '' տ . | |
| ݵ ˼ , and , 'c' ՠ. |
>>> 'a' or 'b''a' >>> '' or 'b'
'b' >>> '' or [] or {}
{} >>> def sidefx(): ... print "in sidefx()" ... return 1 >>> 'a' or sidefx()
'a'
| or ȍ, ˼ ը and , . , or 畸 ՠ. , 'a' տ . | |
| or˼ '', 졒, 'b', , 'b' ՠ. | |
| ݵ , or ՠ. or '' 졒, [] , , {} , {} ՠ. | |
| ˼ or˼ ը տ ̣ , һ Ǹ̂ . ˅ ˼ . , sidefx﹚ ̉ , Ҁ or 'a' 졒, 畸 'a' ՠ챉 ը. |
C , ˼ Ӳ 'bool ? a : b' ւ , ס˼ bool , a 졒 b . and or վը, ˼ ˼ ˅.
Example 2.18. Introducing the and-or trick
>>> a = "first" >>> b = "second" >>> 1 and a or b'first' >>> 0 and a or b
'second'
| ը˼ C ˂ 'bool ? a : b' . ˟̹ ˼ , and ˜ . 1 and 'first' 'first', 'first' or 'second' 'first' . | |
| 0 and 'first' 0 , 0 or 'second' 'second' . |
, ˂ ˼ Ғ, ݲ˂ 禡 Ղ, ˂ and-or Ӏ C ˂ 'bool ? a : b' ը ȍ һ˂ , , ǵ ˍ. a˂ , ˼ ȗ . (˼ ȡ Ҳ 킯 ˅? տ ȗ?)
Example 2.19. When the and-or trick fails
>>> a = "" >>> b = "second" >>> 1 and a or b'second'
| a ըՂ, ס ˼ ը 煂Ղ, 1 and ''˼ '' , '' or 'second' 'second' . ! ס˼ . |
| and-or Ӏ, bool and a or b, a ը C 'bool ? a : b' . | |
, and-or Ӏ ˂ Ӏ 騸 a˂ 浵 . ՠ˞ վ˼ a [a] ಔ ҅ b into [b] ಔ ҅, ՠ ˂ տ ̑ 뵴, ס˼ a b .
Example 2.20. Using the and-or trick safely
>>> a = "" >>> b = "second" >>> (1 and [a] or [b])[0]''
| [a] ˅ ˼ Ղ, ס˼ . a 0 '' ˼ 皦, [a] Ҁ ס˼ һ˂ 皱 ը. |
皱, Ӏ ס 皫 鞵 皵 . ˼, , if йը ˼ , ˉ ? ˸, ˼ , ˼ ˂ ȗ ȍ 졒, ˼ ը ȍ ˅ ۂ , Ҁ ˼ a һ ȗ 豉 ը. ˉ ˟ ȍ ˅ 皦, ̾܂ ˅; ֫(lambda) ﹚ , Ы if йը ˅.
ܲ
˼ ˍ˅ ը ϡ嵴 ˼ ס -﹚ ˂ ˅. , lambda ﹚ ﹚ һ ȍ ˅.
Example 2.21. Introducing lambda functions
>>> def f(x): ... return x*2 ... >>> f(3) 6 >>> g = lambda x: x*2>>> g(3) 6 >>> (lambda x: x*2)(3)
6
| ˼ lambda ﹚ ˤ˂ ȗ˞ ﹚ ˼ . ǵ ը 즗: 빚 ˤ 飡 , return ˅ (ס˼ ¸˞, Ҁ ˟̹ ﹚ һ˂ ˅ ը). , ﹚ , һ ס˼ ̉ ˅. | |
| ˼ 皲 lambda ﹚ 桒 ס ȍ ˅. ȗ ˌ ﹚ , ס˼ ֫ - ﹚ 煡 ˅. |
ՠЂ, lambda ﹚ ҹ˂ 빚 ̑ (˞ 빚 ւ) ˂ ՠ ﹚. lambda ﹚ ჲ ւ һ ȗ˂ ւ . ˼ lambda ﹚ ̈ ҅硒 ; ˉ , ȗ˞ ﹚ ˂ 쵵 즗.
| lambda ﹚ ˂ ը. ס굎 ȍ ˞ 湚˞ ˼ ; ס굎 ȍ ˅ һ, ˼ ˂ ȗ˞ ﹚ ˂졒 ȍ ˅. һ , ˍȍ Ђ졒 ס굎 ȍ ˼ ˼ ﹚ һ˂ . | |
Example 2.22. lambda functions in apihelper.py
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
҄煱 皡 ˅.
, and-or Ӏ ȍ, ס˼ ȗ , Ҁ lambda ﹚ ը ȗ ը. (ס˼ lambda ﹚ ՠ ˼ . ﹚ ȗ ; ﹚˂ ՠ ˼ ˅.)
, split ﹚ 빚 ȍ졒 ˅. ˼ ס 1 ˼ 2˂ 빚 ȍ ˅, һ 빚 ס˼ Վ 僡 .
Example 2.23. split with no arguments
>>> s = "this is\na\ttest">>> print s this is a test >>> print s.split()
['this', 'is', 'a', 'test'] >>> print " ".join(s.split())
'this is a test'
| ˼ ը뵴, ȕ ̉ ը ˂. \n ; \t ը. | |
| 빚 split Վ 僡. ˂ Վ, , ը ݵ . | |
| ˼ ը 僡ҡ һ ס ˂ Վ ͂븇 Վ ˅. - ˂ doc string ˤ help﹚ . |
help ﹚ lambda ﹚, splits, and-or Ӏ 皡 Dz 졒 ˅ ?
Example 2.24. Assigning a function to a variable
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
processFunc ﹚, ﹚ collapse ˂ ˅. collapse , processFunc(string) Վ ˂ Վ ̈ ; , processFunc(string) ˼ ˂ 빚 ՠ .
琲 ֡ , ݲ , ˼ ﹚ һ 硒 , ﹚ ը һ һ˂ collapse 빚 ̑ if йը ȍ ˂ Վ ̈ ۂ졒, ˞ ՠ. ̾܂, Ҁ ﹚ ݵ ፲ ˅ ը; ס ̉ , ס˼ ˅ ˟ ˂ Վ ̈ ۂ ˅ . , ˼ ﹚ Ғ ˅ ˂ 騂 ( ס) 煵 ̧ ˍ ֫ ﹚ ˂ ˅. ˞, , , -ࡢ--빚-ՠ--ꡜ ˼ ˂ ȫ ˅.
ܲ
ֱ ܂ ˼ 삄 , ˂ , ݵ . һ 皱 ˼ , Ҁ ݵ ˼ վ 篼 ˅ ը. ݵ ґ 禡˼ қ; ס굎 ҄ ̡.
Example 2.25. The meat of apihelper.py
print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList])
˼ , ˂ ჲ 즗, һ ס˼ ¹ ը ȍ (“\”). Վ ȍ 桒 ˼ 僡Ҳ ˅ ȡ Ȃ ? տ˼ һ, Ҁ ˟̹ ˂ ˅ ը.
, ӬȲ ± . ˸˂
for method in methodList
տ ˅. ̂, methodList object 皫 ݵ ˅ . method 졒 ˅ .
Example 2.26. Getting a doc string dynamically
>>> import odbchelper >>> object = odbchelper>>> method = 'buildConnectionString'
>>> getattr(object, method)
<function buildConnectionString at 010D6D74> >>> print getattr(object, method).__doc__
Build a connection string from a dictionary of parameters. Returns string.
| help ﹚, object 졒 ˅ ̹, 빚 ҄翫. | |
| methodList 졒 ˅ , method ˍ ˂ . | |
| getattr ﹚ ȍ, object ݵ ˅ method ﹚ յ. | |
| , ˂ doc string ̉ ˼ . |
˂ ˸ 禡˼ doc string˂ ˜ str ȍ . 皵 , str ˌ﹚ ը ϼ. һ doc string˼ ȗ ը, ᱵ str ﹚ 圡 ? ˼ ݵ ﹚ doc string ˼ 첔, , ˂ __doc__ ˼ None .
Example 2.27. Why use str on a doc string?
>>> {}.keys.__doc__>>> {}.keys.__doc__ == None
1 >>> str({}.keys.__doc__)
'None'
| ȍ˟˂ keys ﹚ doc string , ˂ __doc__ ˼ None. ﵵ, __doc__ ˞ , ˂ IDE ǡ굵 啲 嵴, ס ࡢ ˅ , һ ˟ . | |
| ˼ __doc__ ˂ None ˞ ҂븇 ˅. | |
| str﹚ null ̑ ס˂ ը , 'None' ՠ. |
| SQL, ˼ null ҂챉 ˤ = NULL IS NULL ȍ . , ը ; ˼ 坡 顡 == None ȍ. | |
騸 ը 皡 Ղ, ը processFunc ҄ ˅, ס ﹚ ˂ 嵴 ﹚ ˂ Վ ̈졁һ ˼ ̈ . ˼ None ը 챉 ˤ str ȍ 킯. processFunc ը 빚 ۂ졒 ˅ ˂ split ̉嵴, None ҄篫 ס ̾ . Ҁ None˼ split 汉 ը.
禱 һ, ˼ ը ȍ method'˂ ljust ՠ processFunc˂ ՠ ¡졒 ˅ . ˼ ˟ ț ը .
Example 2.28. Introducing the ljust method
>>> s = 'buildConnectionString' >>> s.ljust(30)'buildConnectionString ' >>> s.ljust(20)
'buildConnectionString'
| ljust ը 煲 ̧ Վ . help ﹚ ȍ ˂ ̉ տ ݵ doc string ဂ. | |
| 煲 ը˂ , ljust ը ՠ . ס˼ ը ˧ . |
˂ ҵ. ljust Վ ˂ processFunc ̉ ( ˂ Վ ̈) doc string 煲翫, ¡ ˂ ը . methodList ߱ Ղ, ը˂ 翫. ը "\n"˂ join ȍ, ˂ ը ͂, ˂ ˂ , ̉傄.
>>> li = ['a', 'b', 'c'] >>> print "\n".join(li)a b c
| ˼ ւ ՚ Ӏ. , ˼ ȗ ւ. |
˂ 禡. ˟ 킯 .
Example 2.30. The meat of apihelper.py, revisited
print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList])
apihelper.py ᴡ ˂ ̉˼ ˟ 킯 .
def help(object, spacing=10, collapse=1): """Print methods and doc strings. Takes module, class, list, dictionary, or string.""" methodList = [method for method in dir(object) if callable(getattr(object, method))] processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s) print "\n".join(["%s %s" % (method.ljust(spacing), processFunc(str(getattr(object, method).__doc__))) for method in methodList]) if __name__ == "__main__": print help.__doc__
Example 2.32. Output of apihelper.py
>>> from apihelper import help >>> li = [] >>> help(li) append L.append(object) -- append object to end count L.count(value) -> integer -- return number of occurrences of value extend L.extend(list) -- extend list by appending list elements index L.index(value) -> integer -- return index of first occurrence of value insert L.insert(index, object) -- insert object before index pop L.pop([index]) -> item -- remove and return item at index (default last) remove L.remove(value) -- remove first occurrence of value reverse L.reverse() -- reverse *IN PLACE* sort L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1
˸ ˌ ˟, ݵ 嵴 還 즗:
[3] ˸, ˂ ݵ . ˞, ˂ ̹ ը , һ ˼ ˂ ˂ ̹ ˅. ˼ ݵ 3 ˌ Տ .
| << 1 ˌ 킯챉 | 3 ˌ ̹皂 >> |