* 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__
참조
- 이글루 백업 프로그램 - 배포는 어떻게 하지? (10)2007/09/29
- NoSyu에게 있어 프로그래밍의 시작과 끝 (2)2007/09/27
- Perl OOP 쓰기 힘들다. (10)2007/08/12
- dive into python chapter 4 요약 (4)2007/07/22
- PHP MYSQL 준비된 문장을 이용한 함수 (0)2007/07/10
- RFC보다는 매뉴얼을 참조하자. (0)2007/05/14
- 제가 만드는 프로그램 Test중입니다. (11)2007/05/14
글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.







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