2007년 07월 22일
dive into python chapter 4 요약
* type 함수 : 데이터 타입을 반환하는 함수
예)
>>> type(1)
<type 'int'>
* str 함수 : string으로 변환
예)
>>> str(1)
'1'
* dir 함수 : object의 methods를 list의 attributes로 반환
예)
>>> li = []
>>> dir(li)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
* callable 함수 : call 가능하면 true, 아니면 false를 반환
* getattr 함수 : object.name의 값을 반환
예)
getattr(X, 'foobar')는 X.foobar와 같다.
이 함수는 run-time에서 쓸 수 있기에 매우 유용하다.
예)
>>> g = lambda x: x*2
>>> g(3)
6
list filtering syntax
[mapping-expression for element in source-list if filter-expression]
예)
>>> li = ['a', 'mpilgrim', 'foo', 'b', 'c']
>>> [elem for elem in li if len(elem) > 1]
['mpilgrim', 'foo']
위의 것들이 다 쓰인 예제
def info(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 info.__doc__
참조
# by | 2007/07/22 19:29 | in Programming | 트랙백 | 덧글(4)















☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
허억!
허어억!
허어어어어어어어어억!!!!!!!!!
...............숨이 막히는 순간이군요;;(...)
그럼 전 하악~(응????)
연습장에 간단히 관련 글을 적었는데,
따로 공책을 마련하지 못해서
이렇게 블로그에 글을 남겼습니다.
요즘들어 플래쉬라거나 이런저런거 배움에 불구하고
저런 구조의 글을 보면 일단 앞이 깜깜(...;; 해서;;)
저건 다른 경로를 통해 배운 사람도 헷갈릴거에요.
저만의 노트라서..^^
원래 다른 사람 노트를 이해하는게 교재 이해하는것보다 더 어렵잖아요.^^