BLOG ARTICLE python 3.0 | 2 ARTICLE FOUND

  1. 2008.12.24 Python 2to3 스크립트 간단한 사용법 1
  2. 2008.12.04 python 3.0 릴리즈

간단하게 2.x에서 3.0으로 변환이 잘 되는지 테스트해보기 위해 다음과 같이
test.py 파일을 작성해 보겠습니다.

def div(a):
  print u"Result :", a/2
num = raw_input("input any number :")
div(int(num))

그럼 test.py 파일을 2to3.py를 이용해서 변환을 해 보겠습니다.

C:\Python30\Tools\Scripts>2to3.py -w test.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
--- \Python30\test_.py (original)
+++ \Python30\test_.py (refactored)
@@ -1,4 +1,4 @@
 def div(a):
-  print u"Result :", a/2
-num = raw_input("input any number :")
+  print("Result :", a/2)
+num = input("input any number :")
 div(int(num))
RefactoringTool: Files that were modified:
RefactoringTool: \Python30\test_.py

참고사항.
2to3에 -w 옵션을 주지 않으면 어떻게 변경해야하는지만 화면에 출력하고, 파일에 직접 수정을 가하진 않습니다.

이제 아래와 같이 바뀐 것을 확인할 수 있습니다.

C:\Python30\Tools\Scripts>type test.py
def div(a):
  print("Result :", a/2)
num = input("input any number :")
div(int(num))

다음과 같이 실행도 잘 되는 것을 확인할 수 있네요.

>test.py
input any number :3
Result : 1.5


간단한 프로그램은 이렇게 쉽게 변환이 되지만,
큰 모듈의 경우에는 테스트케이스를 견고하게 짜서 오동작을 하는지 안하는지 체크하는 것이 필수적으로 필요할 것입니다.

또한 2.5 이하의 버전의 코드를 3.0으로 포팅하려면,
우선 2.6으로 코드를 포팅하고, 2.6에서 -3 옵션을 붙여서 실행 한 후, 경고를 처리합니다.
공식문서에서는 위 작업을 완료한 후에 2to3을 사용하는 것을 권장하고 있습니다.


메리 크리스마스~~~
AND

python 3.0 릴리즈

개발/python 2008. 12. 4. 15:35
두둥...

python 3.0이 드디어 릴리즈 되었습니다.

http://python.org/download/releases/3.0/

rc3가 나온지 며칠 되지 않아서 곧바로 업뎃 되네요...

2.x와 3.0의 차이는 다음의 문서를 보면 알 수 있습니다.

http://docs.python.org/dev/3.0/whatsnew/3.0.html

휴우 2.x에 익숙해져 있었는데 3.0에 적응하려면 좀 시간이 걸리겠네요~
AND