Python 스크립트 파일

파일로 저장하고 실행하기

텍스트 편집기를에서 파일이름 happy.py 라는 파일을 만듭니다. Happy.py는 하나의 print 명령문으로 구성된 파일이다.

print("I'm happy!")

터미널을 열고 happy.py 를 저장한 폴더로 이동한다.

$ cd ~/deeplearning
$ python happy.py
I'm happy!

 

클래스

클래스를 정의하면 독자적인 자료형을 만들수 있다.

클래스의 구조는 다음과 같다.

class class_name:
  	def __init__(self, ...):	
      ...
    def method_name(self, ...):
      ...
    def method_name(self, ...):
      ...  

__init__ : 클래스를 초기화 하는 방법을 정의, 생성자(constructor)라고도 함.

다름의 스크립트를 greeting.py 파일로 저장한다.

class greeting:
    def __init__(self, name):
        self.name = name
        print("Initialized!")

    def hello(self):
        print("Hello " + self.name + "!")

    def goodbye(self):
        print("Good-bye " + self.name + "!")

m = greeting("Minsu")
m.hello()
m.goodbye()

터미널에서 greeting.py를 실행한다.

$ python greeting.py
Initialized!
Hello Minsu!
Good-bye Minsu!

greeting 클래스에서 m이라는 객체를 생성한다. greeting의 생성자는 name이라는 인수를 받고 인스턴스 변수인 self.name을 초기화 한다. 인스턴스 변수는 인스턴스별로 저장되는 변수이다.

 
반응형

'딥러닝' 카테고리의 다른 글

유데미 AI 완전정복을 위한 딥러닝 튜토리얼 part 1 수강후기  (0) 2022.01.16
2. python 기초  (0) 2020.03.28
1. 파이썬 설치  (0) 2020.03.28