* 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에서 쓸 수 있기에 매우 유용하다.

 

* lambda 함수

예)

>>> 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__
 

 

참조

http://www.diveintopython.org

http://docs.python.org

크리에이티브 커먼즈 라이선스
Creative Commons License
TAG

글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.

트랙백 주소 :: http://nosyu.pe.kr/trackback/977

댓글을 달아 주세요

  1. Master-PGP 2007/07/22 21:49  댓글주소  수정/삭제  댓글쓰기


    허억!
    허어억!
    허어어어어어어어어억!!!!!!!!!

    ...............숨이 막히는 순간이군요;;(...)

  2. NoSyu 2007/07/22 22:08  댓글주소  수정/삭제  댓글쓰기

    /Master-PGP/
    그럼 전 하악~(응????)

    연습장에 간단히 관련 글을 적었는데,
    따로 공책을 마련하지 못해서
    이렇게 블로그에 글을 남겼습니다.

  3. Master-PGP 2007/07/23 00:51  댓글주소  수정/삭제  댓글쓰기

    (...으음; 수고하셧습니다;;)
    요즘들어 플래쉬라거나 이런저런거 배움에 불구하고
    저런 구조의 글을 보면 일단 앞이 깜깜(...;; 해서;;)

  4. NoSyu 2007/07/23 01:02  댓글주소  수정/삭제  댓글쓰기

    /Master-PGP/
    저건 다른 경로를 통해 배운 사람도 헷갈릴거에요.
    저만의 노트라서..^^
    원래 다른 사람 노트를 이해하는게 교재 이해하는것보다 더 어렵잖아요.^^

[로그인][오픈아이디란?]