파이선은 인터프리터
사실 이것이 가장 강점인데... (최근의 언어들은 인터프리터 환경을 지원하며 아주 좋습니다)
>>> : 이것이 인터프리터 프롬프트입니다.
여기에 한줄씩 입력하면 바로바로 결과가 튀어나옵니다.
>>> 1 + 1
2
이런 것도 가능합니다.
>>> list = [ 1,2,3,4,5]
>>> [ x * 2 for x in list ]
[2, 4, 6, 8, 10]
>>> for i in _:
... print(i)
...
2
4
6
8
10
에디터를 켜지 마세요 인터프리터에 양보하세요.
컴퓨터 언어를 배우기 가장 좋은 환경은 인터프리터입니다.
인터프리터란 컴퓨터와 대화하는 것으로 파이선 인터프리터에서는 키보드 몇번 뚜들기면 "아하" 가 연발합니다.
혹 어렸을 때 BASIC 을 배운적이 있다면 BASIC 이 인터프리터라는 사실을 기억할 것입니다.
파이선을 스크립트 언어라고도 합니다.
Shell 스크립트 대신에 쓸 수 있는데 Perl, Python 등의 언어로 스크립트를 만들 수 있습니다.
장고를 사용한 파이선 스크립트 작성 예입니다.
https://steemit.com/programming/@agile/python-script-django
참고로 MS 윈도우 계열에서는 DOS 시절 제공되는 Batch 파일과 cscript 등의 스크립트를 지원합니다.
맥에서도 AppleScript 라는 것을 지원하며, Batch 작업(일괄작업)을 도와줍니다.
맥도 유닉스 계열이므로 shell 스크립트를 지원하며 윈도우10부터는 bash 을 설치하여 유닉스 shell 스크립트를 쓸 수 있습니다.
shell 스크립트의 예입니다. https://www.macs.hw.ac.uk/~hwloidl/Courses/LinuxIntro/x945.html
저의 경우에는 잘 쓰지 않다 보니 문법도 생소합니다. 읽을 수는 있으나 작성할 수 없는 수준입니다.
#!/bin/bash
if [ $# -lt 1 ]
then
echo "Usage: $0 file ..."
exit 1
fi
echo "$0 counts the lines of code"
l=0
n=0
s=0
for f in $*
do
l=`wc -l $f | sed 's/^\([0-9]*\).*$/\1/'`
echo "$f: $l"
n=$[ $n + 1 ]
s=$[ $s + $l ]
done
echo "$n files in total, with $s lines in total"
설명을 붙이자면 위 파일을 cl.sh 로 저장하고
$ chmod +x cl.sh
$ ./cl.sh file1 file2
를 수행하면 아래와 같은 출력이 나옵니다.
./cl.sh counts the lines of code
file1: 206
file2: 56
2 files in total, with 262 lines in total
넘 어렵네요.... Perl 로 바꿔 보겠습니다.
#!/usr/bin/perl
use strict;
sub trim($) {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
if ( @ARGV < 1 ) {
printf "Usage: $0 file ...\n";
exit(1);
}
printf "$0 counts the lines of code\n";
my $fc = 0;
my $tot = 0;
foreach my $f (@ARGV) {
my $l = `wc -l $f`;
my @w = split ' ', trim($l), 2;
printf "$w[1]: $w[0]\n";
$fc++;
$tot += $w[0];
}
print "$fc files in total, with $tot lines in total\n"
함수도 보이고, 익숙한 printf 도 보이고, 하지만 변수명이 $@ 암호와 같습니다.
가독성면에서 shell 스크립트 보다는 나아진것 같기도 하고 아닌 것 같기도 하고...
저도 한때 Perl 을 좀 썼었는데, 지금은 전혀 기억이 나지 않는군요.
위 코드 짜는데 언 30분 이상 소모 되었습니다. (생산성 BAD)
이제 Python 으로
#!/usr/bin/python
import sys
def line_count(_file):
return sum(1 for l in open(_file))
if len(sys.argv) < 2:
print "Usage: {} file ...".format(sys.argv[0])
exit(1)
print "{} counts the lines of code".format(sys.argv[0])
fc = 0
tot = 0
for i, _file in enumerate(sys.argv):
if i > 0:
lc = line_count(_file)
print "{}: {}".format(_file, lc)
fc += 1
tot += lc
print "{} files in total, with {} lines in total".format(fc, tot)
훨씬 더 읽기 좋습니다. 암호와 같은 $%# 가 사라졌습니다.
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
스크립트 파일은 텍스트 파일이므로 첫 줄에 어떤 스크립트 툴이 라를 실행시킬건지 첫줄에 적습니다.
Seven Languages in Seven Weeks 라는 책이 있습니다.
한가지 언어만 배우는 것보다 여러가지를 섞어 배우면 더 해깔리고 더 맨붕이 오는 즐거움이 옵니다.
if you could give your post an english translation am sure i will learn a lot.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Why don't you use Google translator?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
확실히 파이썬이 자바보다 코드양이 적고 간편하게 나와있는거 같습니다. 근데 자바를 자주 사용하다보니 지금은 자바가 좀더 편한거 같습니다
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit