<< 靈 1 � �健�槪 健��� � 量 靈 3 � ∥譴�� �屬巾�� >>

靈 2 �. ↑�� �

2.1. ヴ健�� ��≠�

健 �鋼 �健�� �悚燁 �寧況 ヴ�ヴ: ↑�. 윈壽솥健 꿱ヴ맸�, �健�읒밀 匯� ↔鋼 ∥譴健ヴ, 그靴� ↑�ュ 回匯靴읒 �ュ ヴ患 匯�� ���槪 ∥譴頌 力�숩�, 그�읒 �� 穢숩況 寓돗��, 그靴� 그↔�槪 ヴ�ュ ��健ヴ. 그 �穢燁읒, �靴ュ 健華꼬健 ��況 穢���, 뮈밀꼬ュ 乾��頌 ��況 繇���, 그靴� ���ュ 晄� 그 健華槪 꿱� �� 鰥�ュ ���槪 �獵� ↔健ヴ.

윈�읒 잽��, 虔돛�ュ �健� �頌그少健 �ヴ. 윈壽솥鋼 健↔槪 숩�梟 �도 剛 健�� � ��꺌 �ヴ. 밝腱≠ 숏윈玲 �乾�鋼 �健�槪 健���읒밀 ヴ��玲 ⌒��槪 숩윈影ヴ. 寧蛔� ��≠ ≫寧∽ 숩윈도 ‘穢�� 譁�; 윈壽솥鋼 健�槪 ��밀 그 匯� ↔�槪 ��∽ � ↔健ヴ.

Example 2.1. apihelper.py

윈壽솥健 껍曳 그峀∽ �� 鰥�ヴ�, 윈壽솥鋼 健↔� 健 甄읒 ����玲 ヴ患 잇靈�槪 蘆淞 良槪 � �ヴ.(Windows, UNIX, Mac OS).

def help(object, spacing=10, collapse=1): 1 2 3
    """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__":                4 5
    print help.__doc__
1 健 匯�鋼 � ⌒� ��況 ≠玲ヴ, help. 그↔� �� 묽꿇읒 �花�, 그↔鋼 뭐 ⌒� �⌒순�況 ��ヴ: object, spacing, 그靴� collapse. 譁�恢 듬 ⌒ュ �靈頌ュ 묽�� �⌒순�乾뎬, 그↔槪 �靴ュ 兩∽ ��섰 ↔健ヴ.
2 help ��ュ 윈壽-娛� 珞밀優 珞腱읽 (doc string)槪 ≠�ュ뎬 그↔鋼 그 ��� ��槪 。��∽ ���ヴ. 껍朗塑 �橒』도 꿇급�� 꿩뀜舡槪 ����; 健 ��ュ 』健 껍レ� 그 湧�況 胛�밀梟 ��� ↔健ヴ.
3 그 �� 꿴� ��ュ �윈꾄� �� �ヴ.
4 "if __name__" ��頌 健 �頌그少鋼 덱潢�劍頌 �� � ┐, ヴ患 �頌그少槪 胛� 匯�頌 ���ュ �도況 籃�� 꼬健, 欖≠ 傑�� ↔槪 � � �ヴ. 健 ±�읒ュ, 그 �頌그少鋼 �뮈尤 help��� doc string槪 �悚�ヴ.
5 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.

2.2. 묽�� 그靴� 健華�ュ 乾�

�健�鋼 �� 乾��健 �셜 』槪 ≠�도刷 ���ヴ; ��≠ 乾� 꼬健 숄淞玲ヴ�, 그 乾�ュ 腱�� �셜 』槪 ��ヴ. ∽ヴ≠, 乾��鋼 健華 �ュ 乾��槪 ���劍頌� �┱ 뮈밀�도 �穢� � �ヴ. SQL 밀�� Transact/SQL읒 ��들 �頌맸葉�鋼 健峀∽ � � �ヴ; 윈壽솥健 SQL밀�� 맑�潢� �珞≠��, 윈壽솥鋼 健 �槪 휸簫� � �ヴ.

Example 2.4. help, a function with two optional arguments

def help(object, spacing=10, collapse=1):

spacingcollapseュ 묽��健ヴ, ���� 그↔�鋼 穢�들 �셜 』槪 ≠�� ┐珞健ヴ. objectュ ���健ヴ, ���� 그↔鋼 껍朗塑 �셜』槪 ≠�� 꿩� ┐珞健ヴ. 梟� help≠ 읫曳 �寧� 乾�頌梟 繇�들ヴ�, spacing10槪 �셜』劍頌 �� collapse1槪 �셜』劍頌 �ヴ. 梟� help≠ 듬 ⌒� 乾�頌 繇�들ヴ�, collapseュ 윈�尤 1槪 �셜』劍頌 �ヴ.

윈壽솥健 collapse읒 �寧� 』槪 �穢��況 읏��梟 spacing槪 胛� �셜 』鋼 良껍�健�況 읏�� 꿩ュヴ� �숩腱. �쇰솥� 꿇�읒밀, 윈壽솥鋼 숄�� ↔健ヴ, ���� 윈壽솥鋼 � ⌒� 乾�頌 그 ��況 繇��꺌梟 � ↔健� ┐珞健ヴ. 그壽寧 �健�읒밀ュ, 乾�ュ 健華劍頌 �┱ 뮈밀�도, �穢� � �ヴ.

Example 2.5. Valid calls of help

help(odbchelper)                    1
help(odbchelper, 12)                2
help(odbchelper, collapse=0)        3
help(spacing=15, object=odbchelper) 4
1 �⌒� 乾�梟劍頌ュ, spacing鋼 �셜 』乾 10槪 ≠�� collapseュ �셜』乾 1槪 ≠玲ヴ.
2 듬 ⌒� 乾���, collapseュ �셜』乾 1槪 ≠玲ヴ.
3 윈�읒밀 윈壽솥鋼 collapse乾�況 �맸�劍頌 ���� 그� 』槪 �穢�ヴ. spacing鋼 윈�尤 그� �셜』乾 10槪 ≠玲ヴ.
4 ���ュ ���乾 乾�獵量도 (object잴  鋼, 그↔鋼 껍朗塑 �셜 』槪 ≠�� 꿩ュヴ) 健華��� � �ヴ, 그靴� 健華�ュ 乾�ュ �┱ 뮈밀�도 寧�陵 � �ヴ.

乾�ュ �뮈尤 ��健�ュ ↔槪 윈壽솥健 ��∽ �� ���ュ 健↔鋼 穢恍頌 健��∽ 숩件 ↔健ヴ. 乾� 健華 꼬健 ��況 繇��ュ “穢��乾” 籃輛鋼 �靈頌ュ �� �華굶健�밀 ��읒밀 �健�鋼 그 』�槪 �� 묽꿇읒밀 �穢들 뮈밀頌 乾��� 健華� 會偈� 셜ヴ. 그靴� �쇰솥� 맸。槪, 윈壽솥鋼 “穢��乾” 籃�劍頌 ���槪 繇�� ↔健ヴ, 그壽寧 ���ヴ� 윈壽솥鋼 쇰≠�乾 傑읓뭡槪 �� ≠玲ヴ.

Note
��況 繇��� 胛�밀 윈壽솥健 �꺌� 傑件� ↔鋼 』槪 (�┍∽ �밀��) �깬�ュ 、 乾�읒 �穢�ュ ↔健ヴ; 그峀∽ �ュ 籃�� 뮈밀ュ 윈壽솥읒∽ マ淞 �ヴ.

� 居�꺌 � ↔

2.3. type, str, dir, 그靴� ヴ患 蘆� ���

�健�읒ュ 껍� 傑�� 蘆� ��� 虔鋼 裔�健 �ヴ. ヴ患 匯� ���鋼 �⌒�葉밀 匯�頌 ��≤ヴ. 健↔鋼 �靈頌 �도�乾 뒵腱乾 묽�健�ュ뎬, 꿇�� ��쇰況 (��, ��� 샬健曳�  鋼) ヴ患 맑�潢� 꿇�잴  健 쇰�淞�ュ ↔槪 恢�腱 �健�ヴ.

type ��ュ 匯� 巾��乾 ∥譴� 뎬健��槪 �橒�ヴ. ≠�� ��鋼 types 匯�읒 寧읽들ヴ. 健↔鋼 ��(잇靈�件) ���읒 傑��밀 윈壽 暎�� 뎬健� ��槪 庚靴� � �ヴ.

Example 2.6. Introducing type

>>> type(1)           1
<type 'int'>
>>> li = []
>>> type(li)          2
<type 'list'>
>>> import odbchelper
>>> type(odbchelper)  3
<type 'module'>
>>> import types      4
>>> type(odbchelper) == types.ModuleType
1
1 type 鋼 �┱ ↔健�도 ��밀 그↔� 뎬健��槪 �橒�ヴ. 蘆≠ ┥�ュ ↔鋼 匯� ↔健ヴ. 穢�(integers), 珞腱읽(strings), 靴맑�(lists), ��(dictionaries), ��(tuples), ��(functions), �甦맑(classes), 匯�(modules), ���ュ �(types)�� 匯� ↔槪 ┥�ヴ.
2 type 鋼 순�況 ��밀 그� 뎬健��槪 �橒� � �ヴ.
3 type 鋼 �� 匯�읒도 虔돛�ヴ.
4 윈壽솥鋼 types匯�� ���槪 ���밀 ∥譴�� �槪 �귑� � �ヴ. 健↔健 力頌 help ��≠ �ュ ↔健�, 그↔槪 �靴ュ 兩∽ �� 섰 ↔健ヴ.

str 鋼 뎬健�況 珞腱읽頌 ∠靈頌 순橒맸�ヴ. 匯� 뎬健��鋼 珞腱읽頌 ∠靈순橒� � �ヴ.

Example 2.7. Introducing str

>>> str(1)          1
'1'
>>> horsemen = ['war', 'pestilence', 'famine']
>>> horsemen.append('Powerbuilder')
>>> str(horsemen)   2
"['war', 'pestilence', 'famine', 'Powerbuilder']"
>>> str(odbchelper) 3
"<module 'odbchelper' from 'c:\\docbook\\dip\\py\\odbchelper.py'>"
>>> str(None)       4
'None'
1 穢�잴  鋼 �뮈� 뎬健��읒 ��윈, 윈壽솥鋼 str 虔돛�靴�� 잇�� ��, ���� �� 匯� 꿇�花鋼 穢�況 珞腱읽頌 力導� �ュ ��況 ≠�� �� ┐珞健ヴ.
2 그峀�梟, str 鋼 �─� �� �─� ∥읒읒도 虔돛�ヴ. 윈�읒밀 그↔鋼 �靴≠ 獵、獵、 獵潢� 靴맑�읒도 虔돛�ヴ.
3 str 鋼 �� 匯�읒도 虔돛�ヴ. ��� ↔鋼 그 匯�� 珞腱읽 ��健 뒵맑�읒밀� 그 匯�� ±頌健華槪 ����, 그甦밀 윈壽솥� ↔鋼 ヴ況 ↔健�ュ ↔健ヴ.
4 str� 藜崍��梟 燁�� �胛ュ 그↔健 �健�� null 』乾, None읒도 虔돛�ヴュ ↔健ヴ. 그↔鋼 珞腱읽 'None'槪 �橒�ヴ. �靴ュ 健↔槪 help��읒 健�� �뎬, 그↔槪 �靴ュ 兩∽ �� 섰 ↔健ヴ.

�靴� help ��� ��쇰읒ュ 그 ∠悚� dir��≠ �ヴ. dir ��ュ �─� ∥譴�도 그 回���� 뱃뭡�� 靴맑�況 �橒�ヴ: 匯�(modules), ��(functions), 珞腱읽(strings), 靴맑�(lists), ��(dictionaries)... ヴ患 皇鋼 �┱ ↔健�도.

Example 2.8. Introducing dir

>>> li = []
>>> dir(li)           1
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> d = {}
>>> dir(d)            2
['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values']
>>> import odbchelper
>>> dir(odbchelper)   3
['__builtins__', '__doc__', '__file__', '__name__', 'buildConnectionString']
1 li ュ 靴맑�健ヴ, 그甦밀 dir(li)ュ � 靴맑�� 匯� 回���槪 ≠玲 靴맑�況 �橒�ヴ. ��� ↔鋼 �橒들 靴맑�ュ 그 回��� 健華槪, 그 回�� 腱譴≠ 껍レ�, 珞腱읽頌 ���ヴュ ↔健ヴ.
2 d ュ ��健ヴ, 그甦밀 dir(d) 鋼 �� 回���� 健華槪 ≠玲 靴맑�況 �橒�ヴ. ��도 健↔� 燁 �寧ュ, � (keys)ュ, 鋸밍�∽ 숩윈꺌梟 �ヴ.
3 健≒健 玲若頌 �藜�� �ュ ≒健ヴ. odbchelper ュ 匯�健ヴ, 그甦밀 dir(odbchelper) ュ, 蘆� 뱃뭡�槪 ���윈, __name____doc__, 그靴� 윈壽솥健 穢�� ヴ患 匯� 뱃뭡� 回��잴  鋼, 그 匯�읒밀 穢�� 匯� 暎�� ↔�槪 ≠玲 靴맑� �寧況 �橒�ヴ. 健 ±�읒ュ, odbchelper 읫曳 �寧� ��腱-穢� 回��, buildConnectionString ��況 ≠�ュ뎬 �靴ュ �健�槪 健���� �읒밀 ×쇰� 力 �ヴ.

譁�恢劍頌, 繇�≠�� ��ュ �─� ∥譴�도 ��밀 그 ∥譴≠ 繇�≠���, 1槪 �橒�� 그峀� 꿩劍�, 0 槪 �橒�ヴ. 繇�≠�� ∥譴읒ュ ��(functions), �甦맑(class) 回��(methods), ���ュ �甦맑 腱���도 ��들ヴ. (靈 3 �읒밀 �甦맑읒 ��윈 � 腱뭐尤 ヴ�ヴ.)

Example 2.9. Introducing callable

>>> import string
>>> string.punctuation           1
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.join                  2
<function join at 00C55A7C>
>>> callable(string.punctuation) 3
0
>>> callable(string.join)        4
1
>>> print string.join.__doc__    5
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)
1 string 匯�읒 �ュ ���鋼 ��健 霓� 꿩ヴ (皇鋼 �┶�健 윈�尤 join ��況 ���읒도 숄깬��), 그壽寧 그 匯�鋼 健壽� string.punctuation�  鋼 皇鋼 傑�� ���槪 ���� �ヴ, 그 ��ュ 匯� �影�乾 깬듬瑩 珞腱�槪 ���ヴ.
2 string.join ュ 珞腱읽� 靴맑�況 읓�� �ュ ��健ヴ.
3 string.punctuation 鋼 繇�≠��� 꿩ヴ; 그↔鋼 珞腱읽健ヴ. (珞腱읽鋼 繇�≠�� 回���槪 ≠�� ��梟, 그壽寧 그 珞腱읽 腱譴ュ 繇�≠��� 꿩ヴ.)
4 string.join鋼 繇�≠��ヴ; 健 ��ュ 듬 ⌒� 乾�況 ≠�ュ ��健ヴ.
5 繇�≠�� �─� ∥譴�도 doc string槪 ≠� � �ヴ. 繇�≠�� (callable) ��況 � ∥譴� 뱃뭡읒 、、 ���윈, �┱ 뱃뭡읒 ��槪 듬�꺌 ��(methods, functions, classes) 그靴� �┱ ↔槪 朗맸��況 읏�ュ�況 (constants, etc.) , 그 ∥譴읒 ��윈 �┱ ↔도 ��읒 匯花�밀도, �靴ュ �穢� � �ヴ .

type, str, dir, 그靴� �健�� 匯� 寧蛔� 蘆� ���鋼 그���葉 '__builtins__'�� 숄靴ュ �샥� 匯�頌 ��≤ヴ. (그↔鋼 듬⌒� 亂娛槪 꽜� 딘읒 ≠玲ヴ.) 梟� 健↔健 도�健 ��ヴ�, 윈壽솥鋼 �健�健 庚舡 맸虔� ┐ 腱돛�劍頌 'from __builtins__ import *'況 ���ヴ� 휸、� � �ヴ, 그↔鋼 匯� “蘆�(built-in)” ���槪 健華윳을읒 �建�� 그甦밀 윈壽솥鋼 그↔�槪 曳閻�劍頌 ��� � �ヴ.

健塑 籃�劍頌 휸、�ュ 健瑩鋼 __builtins__ 匯�읒 �� 穢숩況 寓돗�劍頌� 匯� 蘆� ��잴 뱃뭡읒 �寧� 그�劍頌 윈壽솥健 閻깜� � �ヴュ ↔健ヴ. 그壽� 꿱껍會偈� 숩�, �靴ュ 그↔槪 胛� �寧� ��況 ≠�� �ヴ; 그↔鋼 help�� 숄靴�ヴ. 맑맑頌 맸도�밀 그 靴맑�況 �� �� 숩�; �靴ュ 健�읒 그 燁�� ���燁 �⌒읒 ヴ健�� ��〈 ↔健ヴ. (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...]
Note
�健�읒ュ �禾� �獵 �ゼ�健 �� 읫ュ뎬, 그↔槪 윈壽솥鋼 잽�尤 穢덱槪 �밀 �健�健 靈×�ュ 匯� 匯��槪 ��꺌梟 �ヴ. 그壽寧 �쇰솥� 꿇�읒밀 윈壽솥鋼 健壽� 匯�槪 ���ュ 輛槪 ヴ맸 ─이靴� 胛�밀 �ゼ�槪 (玗鋼 � �健�, 玗鋼, 뭐�읒寧, MSDN槪) �獵�� �ュ 腱��槪 烙¨�∫�梟, �健�鋼 �쇰솥健 腱譴頌-珞밀優(self-documenting) �� �ヴ.

� 居�꺌 � ↔

2.4. getattr頌 ∥譴� �獵瑩槪 寓돗��

윈壽솥鋼 健藜 �健� ��ュ ∥譴�ュ ↔槪 꿴ヴ. 윈壽솥健 匯花ュ ↔鋼 getattr ��況 ���윈, ��맸�� 그 健華槪 匯花�밀도 � ��� �獵瑩槪 寓돗� � �ヴュ ↔健ヴ.

Example 2.11. Introducing getattr

>>> li = ["Larry", "Curly"]
>>> li.pop                       1
<built-in method pop of list object at 010DF884>
>>> getattr(li, "pop")           2
<built-in method pop of list object at 010DF884>
>>> getattr(li, "append")("Moe") 3
>>> li
["Larry", "Curly", "Moe"]
>>> getattr({}, "clear")         4
<built-in method clear of dictionary object at 00F113D4>
>>> getattr((), "pop")           5
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'pop'
1 健↔鋼 그 靴맑�� pop回��읒 �� �獵瑩槪 寓돗�ヴ. 健↔鋼 그 pop回��況 繇��ュ ↔健 껍レ�ュ ↔槪 ����; 그 繇�鋼 li.pop()健 � ↔健ヴ. 健↔鋼 그 回�� 腱�健ヴ.
2 健↔ �� pop 回��읒 �� �獵瑩槪 �橒�ヴ, 그壽寧 健 ±�읒ュ, 그 回�� 健華健 getattr ��읒 珞腱읽 乾�頌밀 �穢들ヴ. getattr鋼 ��� 穢도頌 傑�� 蘆� ��頌밀 �┱ ∥譴� �┱ 뱃뭡健�도 �橒�ヴ. 健 ±�읒ュ, 그 ∥譴ュ 靴맑�健�, 그 뱃뭡鋼 pop 回��健ヴ.
3 健↔健 �譁寧 傑���≠ 蛔靴뱃읒 剛 꿴 ��이 ±�읒ュ, 健↔槪 맸도� 숩�: getattr� �橒 』鋼 그 回�� 健ヴ, 그壽� 윈壽솥鋼 그↔槪 li.append("Moe")�� 曳閻�劍頌 恍� ↔� �  健 繇�� � �ヴ. 그壽寧 윈壽솥鋼 그 ��況 曳閻�劍頌 繇��� 꿩뀜ヴ; ��읒 윈壽솥鋼 그 ��� 健華槪 珞腱읽頌 �穢�ヴ.
4 getattr鋼 �� ��읒도 虔돛�ヴ.
5 健速�劍頌ュ, ��健 �─� 回��도 ≠�� 꿩ュヴュ 瑩梟 靈��ヴ�, getattr鋼 ��읒도 虔돛� ↔健ヴ, 그甦밀 getattr鋼 윈壽솥健 �─� 뱃뭡槪 娛��도 잇�況 烙휸맸� ↔健ヴ.

getattr ュ �뮈尤 蘆� 뎬健��梟槪 胛� ↔鋼 껍レヴ. 그↔鋼 �� 匯�읒도 虔돛�ヴ.

Example 2.12. getattr in apihelper.py

>>> import odbchelper
>>> odbchelper.buildConnectionString             1
<function buildConnectionString at 00D18DD4>
>>> getattr(odbchelper, "buildConnectionString") 2
<function buildConnectionString at 00D18DD4>
>>> object = odbchelper
>>> method = "buildConnectionString"
>>> getattr(object, method)                      3
<function buildConnectionString at 00D18DD4>
>>> type(getattr(object, method))                4
<type 'function'>
>>> import types
>>> type(getattr(object, method)) == types.FunctionType
1
>>> callable(getattr(object, method))            5
1
1 健↔鋼 odbchelper 匯�읒 �ュ buildConnectionString ��읒 �� �獵瑩槪 �橒�ヴ, 그↔槪 �靴ュ �健�槪 健���읒밀 ×쇰�ヴ. (윈壽솥健 숩ュ 16玲 �뱉ュ 蘆 ���읒 �穢� ↔健ヴ; 윈壽솥� �悚��ュ ヴ況 ↔健ヴ.)
2 getattr況 ���윈, �靴ュ  鋼 ��읒 ��  鋼 �獵瑩槪 寓돗� � �ヴ. 件��劍頌, getattr(object, "attribute") ュ object.attribute� 돛��ヴ. 梟� ∥譴(object)≠ 匯�健��, 그壽� 뱃뭡(attribute)鋼 그 匯�읒밀 穢�들 �┱ ↔健�도 � � �ヴ: ��(a function), �甦맑(class), 玗鋼 �을 순�(global variable).
3 그靴� 健↔健 �靴≠ help��읒밀 �靈頌 ���ュ ↔健ヴ. ∥譴(object)ュ 乾�頌밀 그 ��읒 ��玲ヴ; 回��(method)ュ 珞腱읽頌밀 回�� 玗鋼 ��� 健華健ヴ.
4 健 ±�읒ュ, methodュ � ��� 健華健�, 그↔槪 �靴ュ 그↔� �(type)槪 寓돗�劍頌� ��� � �ヴ.
5 回��(method)ュ ��健�頌, 그↔鋼 繇�≠��ヴ.

2.5. 靴맑�況 �壽蘆�

윈壽솥健 꿱ヴ맸�, �健�鋼, 靴맑� 纜을槪 ��, 靴맑�況 ヴ患 靴맑�읒 羊禮ュ ∠悚� �悚槪 ≠�� �ヴ. 健↔鋼 윈� 回�レ�� ��� � �ュ뎬, ��읒밀 그 靴맑�읒 �ュ �┱ �뱉�鋼 羊�� �� ヴ患 ↔�鋼 잽�尤 휸簫��玲ヴ.

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]       1
['mpilgrim', 'foo']
>>> [elem for elem in li if elem != "b"]         2
['a', 'mpilgrim', 'foo', 'c', 'd', 'd']
>>> [elem for elem in li if li.count(elem) == 1] 3
['a', 'mpilgrim', 'foo', 'c']
1 羊禮� ���鋼 윈�읒밀 �뮈�ヴ (그↔鋼 �뮈尤 、 �뱉� 』槪 �橒�ヴ), 그甦밀 윈� ���읒 裔燁�ヴ. �健�鋼 그 靴맑�況 耘둥健 ��밀, 、 �뱉況 그 윈� ���槪 ��윈 ���ヴ; 梟� 그 윈� ���健 �健��, 그 �뱉ュ 羊��葉밀 그 羊禮� ���� ��ュ �橒들 靴맑�읒 ��들ヴ. 윈�읒밀 윈壽솥鋼 匯� �-珞腱 珞腱읽槪 �壽蘆� �ュ 燁健�, 그甦밀 윈壽솥읒∽ュ 그↔숩ヴ �ヴ� 珞腱읽� 靴맑�≠ 錄�玲ヴ.
2 윈�읒밀 윈壽솥鋼 �샥� 』, b況 �壽蘆ュ 燁健ヴ. ��� ↔鋼 健↔健 b� 匯� ��槪 �壽蘆ュ ↔健�ュ ↔乾뎬, ���� 그↔健 ��� ┐譁ヴ, 그 윈� ���鋼 �禮健 � ↔健� ┐珞健�ュ ↔健ヴ.
3 countュ 靴맑� 回��頌밀 �寧� 』健 靴맑�읒밀 寧�寧ュ 勇�況 �橒�ヴ. 읏甦� 靴맑�읒밀 、 』�읒 �� �寧꼭� 손�셜槪 ���ュ 靴맑�況 �橒�ヴ�, 윈壽솥鋼 健 윈��≠ �寧� 靴맑�頌쇰� 燁손槪 靈�� ↔健�� 휸、� �도 匯患ヴ. 그壽寧 그峀� 꿩ヴ, ���� 읏甦� 靴맑�읒밀 듬 纜 寧�寧ュ 』�鋼 (健 ±�읒ュ, bd) 잽�尤 靈��� ┐珞健ヴ. 靴맑�頌쇰� 燁손槪 靈��ュ 籃輛�健 ��ュ ��梟, 그壽寧 윈�≠ 그 ��甄鋼 껍レヴ.

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__뱃뭡�  鋼, ヴ患 뱃뭡�鋼 ���� 꿩ュヴ.

� 居�꺌 � ↔

2.6. andor� �健� 셜뭡

�健�읒밀, andorュ 윈壽솥健 잇���頌 숄靴꿇 �靴況 ���ヴ, 그壽寧 그�鋼 숄靴꿇 』�槪 �橒�� 꿩ュヴ; 그↔�鋼 �귑�� �ュ �靈� 』� 燁 �寧況 �橒�ヴ.

Example 2.16. Introducing and

>>> 'a' and 'b'         1
'b'
>>> '' and 'b'          2
''
>>> 'a' and 'b' and 'c' 3
'c'
1 and況 ��� ┐, 』�鋼 숄靴꿇 珞穫읒밀 잗臆읒밀 읫患臆劍頌 �≠들ヴ. '0, '', [], (), {}, 그靴� None'鋼 匯듬 숄靴꿇 珞穫읒밀 �禮健ヴ; ヴ患 匯� ↔鋼 �健ヴ.[3] 梟� 匯� 』�健 숄靴꿇 珞穫읒밀 �健��, andュ 譁�恢 』槪 �橒�ヴ. 健 ±�읒ュ, and'a'況 �劍頌, �≠�� 'b'況 �劍頌, �≠�윈 'b'況 �橒�ヴ.
2 梟� �┱ 』健�도 숄靴꿇 珞穫읒밀 �禮健��, and ュ 憬纜櫻 �禮 』槪 �橒�ヴ. 健 ±�읒ュ, '' ≠ 憬 纜櫻 �禮 』健ヴ.
3 匯� 』�鋼 �健ヴ, 그甦밀 andュ 譁�恢 』, 'c'況 �橒�ヴ.

Example 2.17. Introducing or

>>> 'a' or 'b'          1
'a'
>>> '' or 'b'           2
'b'
>>> '' or [] or {}      3
{}
>>> def sidefx():
...     print "in sidefx()"
...     return 1
>>> 'a' or sidefx()     4
'a'
1 or槪 ����, 』�鋼 숄靴꿇 珞穫읒밀 and잴 �  健, 잗臆읒밀 읫患臆劍頌 �≠들ヴ. 梟� �┱ 』健�도 �健�, orュ 그 』槪 �맸 �橒�ヴ. 健 ±�읒ュ, 'a'≠ 憬 纜櫻 � 』健ヴ.
2 or''況, �禮劍頌 �≠��, 그靴�ュ 'b'況, �劍頌 �≠�밀, 'b'況 �橒�ヴ.
3 梟� 匯� 』健 �禮健�, orュ 譁�恢 』槪 �橒�ヴ. or''況 �禮劍頌 �≠��, 그靴�ュ []況 �禮劍頌, �≠��, 그靴�ュ {}況 �禮劍頌, �≠�윈 {}況 �橒�ヴ.
4 ��� ↔鋼 or鋼 숄靴꿇 珞穫읒밀 �乾 憬 纜櫻 』槪 黔槪 ┐��梟 �≠��, 그靴�ュ 그 寧蛔�況 朗맸�ヴュ ↔健ヴ. �┱ 』�健 쇰虔�槪 ≠� � �ヴ� 健壽� �뭡鋼 燁��ヴ. 윈�읒밀, sidefx��ュ 據�頌 繇��� 꿩ュヴ, ���� or'a'況 �劍頌 �≠��, �맸 'a'況 �橒�� ┐珞健ヴ.

윈壽솥健 C ����, 윈壽솥鋼 �煌꼬健 'bool ? a : b' ���읒 鋸밍� ↔健ヴ, 그↔鋼 bool健 �健�, a況 �≠�� 그峀� 꿩劍� b況 �≠�ヴ. �健�읒밀 andor≠ 虔돛�ュ 籃�┐珞읒, 윈壽솥鋼 �  鋼 件槪 マ뭡� � �ヴ.

Example 2.18. Introducing the and-or trick

>>> a = "first"
>>> b = "second"
>>> 1 and a or b 1
'first'
>>> 0 and a or b 2
'second'
1 健 깬珞鋼 C 읒밀� 'bool ? a : b' ���� �며�∽ 숩乾ヴ. �譴 ���鋼 잗臆읒밀 읫患臆劍頌 �≠들ヴ, 그甦밀 and≠ 晄� �≠들ヴ. 1 and 'first'≠ �≠�� 'first'頌, 그靴�ュ 'first' or 'second'≠ �≠�� 'first'頌 들ヴ.
2 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 1
'second'
1 aュ � 珞腱읽健�頌, 그↔槪 �健�鋼 숄靴꿇 珞穫읒밀 �禮劍頌 。���頌, 1 and ''''頌 �≠��, 그靴�ュ '' or 'second''second'頌 �≠들ヴ. 健塑! 그↔鋼 �靴≠ 읏� ↔健 껍レヴ.
Important
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] 1
''
1 [a] ュ �� �� 꿩鋼 靴맑�健�頌, 그↔鋼 據�頌 �禮健 껍レヴ. 梟� a0 �ュ '' 玗鋼 ヴ患 �┱ �禮 』件 ��도, 靴맑� [a]ュ �健ヴ ���� 그↔鋼 �寧� �뱉況 ≠�� ┐珞健ヴ.

����, 健壽� ��ュ 그↔健 �レュ ≠�숩ヴ � ��� 숩件�도 匯患ヴ. 윈壽솥鋼, �균, if 밀�珞劍頌  鋼 件槪 マ뭡�ヴ, 그甦밀 � 健塑 손�� 件槪 �ュ≠? 舡, 皇鋼 ±�읒, 윈壽솥鋼 듬 ⌒� �� 』 �健況 묽�槪 ��, 그甦밀 윈壽솥鋼 � 맞� 깬珞槪 ��� � �ヴ 그靴� ‘穢�� 譁�, ���� 윈壽솥鋼 a』健 꿇靈寧 �� �件 ↔健�ュ ↔槪 꿱� ┐珞健ヴ. 그靴� 윈壽솥健 � 손�� 꿴�� ��況 ��� ��≠ �槪 ��도, ��읒ュ 그峀∽ �꺌� 潔솥� 健傑≠ �ヴ; ┶ヴ(lambda) ��읒밀잴  健, �健�읒밀ュ if 밀�珞健 ���� 꿩ュ ±�≠ �ヴ.

� 居�꺌 � ↔

2.7. lambda��況 ����

�健�鋼 �藜�ュ 깬珞槪 靈×�ュ뎬 윈壽솥鋼 그↔劍頌 �娛若靴 뱉�-��況 巾맸頌 穢� � � �ヴ. 靴맑�頌쇰� 量�� 장, 健壽� 健患力 lambda ��ュ ��≠ ��� �뒵읒밀寧 ��� � �ヴ.

Example 2.21. Introducing lambda functions

>>> def f(x):
...     return x*2
...     
>>> f(3)
6
>>> g = lambda x: x*2  1
>>> g(3)
6
>>> (lambda x: x*2)(3) 2
6
1 健↔鋼 lambda ��頌밀 胛� 穢��乾 ��잴 �  鋼 件槪 マ뭡�ヴ. 윈�읒 그 휸簫들 깬珞槪 ����: 乾� 靴맑� �胛읒ュ 껍朗塑 □繇≠ 꼬ヴ, 그靴� return ���≠ �葉 �ヴ (그↔鋼 覽맸�健ヴ, ���� �譴 ��ュ 읫曳 �寧� ���健 � � �� ┐珞健ヴ). ��, 그 ��ュ 健華健 꼬ヴ, 그壽寧 그↔鋼 腱�健 ��들 그 순�況 ��윈 繇�� � �ヴ.
2 윈壽솥鋼 ���ュ lambda ��況 순�읒 ���� 꿩�도 그↔槪 ��� � �ヴ. 健 뭐�읒밀 穢恍頌 ≠� 傑�� ��ュ 껍レ�梟, 그↔鋼 ┶ヴ≠ �� �乾-꿴 ���ュ ↔槪 숩윈�� �ヴ.

件�優��, lambda ��ュ �─� ⌒�� 乾��도 ��� (묽�� 乾�況 ���윈) 그靴� �⌒� ���劍頌 들 』槪 �橒�ュ ��健ヴ. lambda ��ュ ���況 ��� � 꼬� �寧 健�� ���槪 ��� � 꼬ヴ. 樓朗 皇鋼 ↔槪 lambda ��頌 꿀�� �劍淞� �� 譁�; 윈壽솥健 英 � 손�� ↔健 ���ヴ�, ��읒 穢��乾 ��況 穢��밀 윈壽솥健 읏�ュ 梟� 굶∽ 虔뭡�도刷 ��.

Note
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"  1
>>> print s
this   is
a	test
>>> print s.split()           2
['this', 'is', 'a', 'test']
>>> print " ".join(s.split()) 3
'this is a test'
1 健↔鋼 윈壽娛 珞腱읽乾뎬, �燁 乾�쇰繇��읒 �� 珞腱읽頌 穢�들ヴ. \nュ �靴� 靴�健�; \t ュ � 珞腱健ヴ.
2 乾� 꼬ュ splitュ ×�槪 �影劍頌 �〉ヴ. 그甦밀 뭐⌒� ×�, �靴� 靴�, 그靴� � 珞腱ュ 匯듬 돛件�ヴ.
3 윈壽솥鋼 珞腱읽槪 �⌒� 그靴� 寧밀 ヴ맸 그↔槪 �⌒� ×�槪 깬솥腱頌 �밀 ���劍頌� ×�槪 �影劍頌 � � �ヴ. 健↔健 力頌 윈壽-娛 �乾� doc string槪 バ壽밀 � 娛頌 梟�� 胛�밀 help��≠ �ュ ↔健ヴ.

그甦밀 help ��ュ 健壽� lambda ��, splits, 그靴� and-or ��況 ≠�� �靈頌 朗�槪 �� �ュ ↔乾≠?

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 밀�珞槪 ���윈 �⌒� ×�劍頌 ���꺌 �� 恍�況 �穢��, 그靴�ュ 그 �據� 』槪 �橒�ヴ. 健↔劍頌ュ 숄潔솥�뎬, ���� 그 ��ュ 匯� ≠�� ±�況 ヴ��꺌� ��뭡健 �� ┐珞健ヴ; 윈壽솥健 그↔槪 繇�� ┐譁ヴ, 그↔鋼 윈壽솥健 읏�ュ ↔槪 윈壽솥읒∽ 娛 � �� �읒 �⌒� ×�劍頌 ���꺌��況 �穢�꺌梟 � ��≠ �槪 ↔健ヴ. �健�읒밀, 윈壽솥鋼 그 ��頌 쇰� 그壽� �穢 �靴況 �槪 � �� 윈壽솥� 읏�ュ 力況 穢蟯�∽ (그靴� 그↔梟槪) �도刷 會偈� ��들 ┶ヴ ��況 穢�� � �ヴ. 健↔健 �� 湧喀�健�, �� �껍��, 그靴� 그壽� 뭡≠�, 蘆-휸、읒-그壽�-乾��健-��頌-�ュ-↔�  鋼 暎�� 읒壽況 � 蘆ュ ±�健 �ヴ.

� 居�꺌 � ↔

2.8. 匯듬 ���

�靴≠ 껍曳�� 솥��� 꿩鋼 傑件� ��乾, 譁�恢 �乾� ��ュ, 그 匯�件槪 � ��健ヴ. 그壽寧 ���� 그 件鋼 릴ヴ, ���� �靴≠ ��� 匯� ↔鋼 健藜 �靴≠ ��� 籃�劍頌 影��� �� ┐珞健ヴ. 匯� 도藜� 獵、�鋼 靈 腱靴읒 �윔�; 그↔�槪 ┐淞밀 ��├奐 맸。健ヴ.

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

槪 숩� �靴ュ 健↔健 靴맑� 纜을健�ュ ↔槪 꿱 � �ヴ. 윈壽솥健 꿱ヴ맸�, methodListobject읒밀 �靴≠ ��槪 ≠�ュ 匯� 回��況 �� �ュ 靴맑�健ヴ. 그甦밀 �靴ュ method頌 그 靴맑�況 耘둥健況 �� �ュ 燁健ヴ.

Example 2.26. Getting a doc string dynamically

>>> import odbchelper
>>> object = odbchelper                   1
>>> method = 'buildConnectionString'      2
>>> getattr(object, method)               3
<function buildConnectionString at 010D6D74>
>>> print getattr(object, method).__doc__ 4
Build a connection string from a dictionary of parameters.

    Returns string.
1 help ��읒밀, objectュ �靴≠ 도�槪 깬�� �ュ ∥譴頌밀, 乾�頌밀 ��玲ヴ.
2 �靴≠ methodList況 耘둥健 �� �ュ 돛꿴읒, method ュ �� 回��� 健華健ヴ.
3 getattr ��況 ���윈, �靴ュ object 匯�읒 �ュ method ��읒 �� �獵瑩槪 寓돗�ヴ.
4 健靈, 그 回��� �靈 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__         1
>>> {}.keys.__doc__ == None 2
1
>>> str({}.keys.__doc__)    3
'None'
1 ��� keys ��ュ doc string槪 ≠�� 꿩ュヴ, 그甦밀 그� __doc__ 뱃뭡鋼 None健ヴ. �〈靴∽도, 윈壽솥健 __doc__ 뱃뭡槪 曳閻�劍頌 �≠�ヴ�, �健�� IDE ュ 껍朗↔도 �� 蘆� 꿩ュ뎬, 그↔읒 �� 휸、� 숩� 件靴≠ ��ュ ��梟, 그壽寧 윈�尤 숄�據�ヴ.
2 윈壽솥鋼 __doc__ 뱃뭡� 』健 �靈頌 None乾≠況 曳閻�劍頌 �귑�劍頌� 蟯乾� � �ヴ.
3 str��ュ null 』槪 ��밀 그↔� 珞腱읽 ��槪, 'None'槪 �橒�ヴ.
Note
SQL읒밀, 윈壽솥鋼 null 』槪 �귑�� 胛�윈 = NULL ��읒 IS NULL槪 ��� ↔健ヴ. �健�읒ュ, 그壽� �샥� 깬珞健 꼬ヴ; 윈壽솥鋼 ヴ患 �┱ �귑읒밀잴 譁�≠�頌 � 健 == None 槪 ���ヴ.

健靈 �靴ュ 蟯��∽ 珞腱읽槪 ≠�∽ ��劍�頌, �靴ュ 그 珞腱읽槪 processFunc읒 �� 娛 � �劍�, 그↔槪 �靴ュ ��頌 健藜 穢� �ュ뎬 그 ��ュ �⌒� ×�劍頌 ����寧 玗鋼 ���� 꿩ュヴ. 健靈 윈壽솥鋼 None』槪 珞腱읽 ��劍頌 순橒�� 胛�윈 str槪 ���ュ ↔健 � 燁��タ≠況 健��ヴ. processFunc ュ 珞腱읽 乾�況 ≠穢�� �� 그靴� 그� split 回��況 繇��ュ뎬, �靴≠ None槪 ��影ヴ� 그↔槪 潔둥槪 件劍� ↔健ヴ. ���� Nonesplit回��況 ≠�� 꿩� ┐珞健ヴ.

딘頌 獵� � 寧껍≠�, �靴≠ 숩ュ ↔鋼 � ヴ맸 珞腱읽 ��優況 ���윈 method'� ljust 回��≠ �橒�ュ 』� processFunc� �橒 』槪 읓��� �ヴュ ↔健ヴ. 健↔鋼 �靴≠ 健�읒 숩� 鰥�タ �頌� 珞腱읽 回��健ヴ.

Example 2.28. Introducing the ljust method

>>> s = 'buildConnectionString'
>>> s.ljust(30) 1
'buildConnectionString         '
>>> s.ljust(20) 2
'buildConnectionString'
1 ljustュ 그 珞腱읽읒 ��玲 굶健읒 會偈� ×�槪 숏乾ヴ. 健↔健 力頌 help ��≠ ���ュ ↔劍頌 듬⌒� �悚 읽槪 梟�� 듬纜櫻 읽읒ュ 匯� doc string槪 穢��ヴ.
2 梟� ��玲 굶健≠ 그 珞腱읽� 굶健숩ヴ 虔ヴ�, ljust �뮈尤 그 珞腱읽槪 그�頌 �橒� ↔健ヴ. 그↔鋼 그 珞腱읽槪 剛�蘆� 꿩ュヴ.

健靈 �� 怒撚ヴ. ljust 回��頌쇰� ×�庚靴들 回��� 健華� 그靴� processFunc읒 �� 繇�頌쇰� (껍譁도 �⌒� ×�劍頌 ����槪) doc string健 ��玲ヴ�, �靴ュ 그 �槪 읓��밀 � ⌒� 珞腱읽槪 �ュヴ. �靴ュ methodList況 羊禮� �劍�頌, �靴ュ 珞腱읽� 靴맑�況 ��頌 ≠玲ヴ. 珞腱읽 "\n"join 回��況 ���윈, �靴ュ 健 靴맑�況 �⌒� 珞腱읽頌 ���ヴ, 그靴� 그 靴맑�� 、 �뱉況 샥⌒� � �乾劍頌, 그 ��況 �悚�ヴ.

Example 2.29. Printing a list

>>> li = ['a', 'b', 'c']
>>> print "\n".join(li) 1
a
b
c
1 健↔鋼 �� 윈壽솥健 靴맑�잴 虔꾑� ┐ 傑�� 뒵�� ��健ヴ. 그靴� �健�읒밀, 윈壽솥鋼 �� 靴맑�잴 虔꾑�ヴ.

健↔健 ��� 譁�恢 獵、健ヴ. 健 ��ュ 健靈 잽�尤 健�≠ �꺌 �ヴ.

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])

2.9. � �

apihelper.py �頌그少� 그� �悚鋼 잽�尤 健�≠ ��꺌 �ヴ.

Example 2.31. 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 � ∥譴�� �屬巾�� >>