2014-01-14 7 views
2

파이썬이 출력의 repr을 인쇄 할 것이라는 것을 알고 있었지만, 분명히 항상 그런 것은 아닙니다. ipython에서ipython과 파이썬의 출력 차이

: 파이썬

In [1]: type([]) 
Out[1]: list 

In [2]: set([3,1,2]) 
Out[2]: {1, 2, 3} 

: 예를 들면, 출력에 적용 ipython 무엇 변환

>>> type([]) 
<type 'list'> 
>>> set([3,1,2]) 
set([1, 2, 3]) 

?

+0

IPython 버전? –

+0

파이썬'2.7.5'에 ipython'1.1.0'이 있습니다. – wim

답변

8

대신 repr 또는 표준 pprint 모듈 IPython은 IPython.lib.pretty.RepresentationPrinter.pretty 방법을 print the output에 사용합니다.

모듈 IPython.lib.pretty은 뒤에서 RepresentationPrinter.pretty을 사용하는 두 가지 기능을 제공합니다.

>>> from IPython.lib.pretty import pprint 
>>> pprint(type([])) 
list 

IPython는 표준 파이썬 pprint 모듈 "때문에 자신의 꽤 프린터를 사용

>>> from IPython.lib.pretty import pretty 
>>> pretty(type([])) 
'list' 

IPython.lib.pretty.pprint 기능은 객체의 표현을 인쇄합니다

IPython.lib.pretty.pretty 함수는 객체의 문자열 표현을 반환 개발자는 자신의 예쁜 인쇄 콜백을 제공 할 수 없습니다. "

+1

실제로'IPython.lib.pretty.pretty'가 실제로 사용되는 함수라고 문서화되어 있습니까? 나는 IPython.lib.pretty.pretty = lambda * args, ** kwargs : 'potato'을했고 ipython의 출력에는 변화가 없었다. – wim

+0

@wim, 좋은 캐치, 내 대답을 편집했습니다. 'IPython.lib.pretty.RepresentationPrinter.pretty = lambda * args, ** kwargs : sys.stdout.write ('12')'는 모든 입력에 대해 12를 출력합니다. –

+1

이것은 '% pprint' 매직이 ON 인 경우에만 해당합니다. 기본적으로 해제되어 OP가 일반 파이썬 프롬프트에서 보는 동작을 얻습니다. – MattDMo

관련 문제