본문 바로가기
728x90
반응형
SMALL

Python기초3

Python - 예외 처리 예외처리는 try ~ except 문을 통해 처리 한다. 기본 문법은 아래와 같다. try: # do something except EOFError: print('why did you do an eof on me?') else: print('No exception') 다른 언어에서 처럼 예외클래스를 custom 하게 만들 수도 있다. 마찬가지로 finally 를 이용하면 예외가 발생하더라도 안전하게 후처리를 할 수 있다. class ShortInputException(Exception): '''A user-defined exception class''' def __init__(self, length, atleast): Exception.__init__(self) self.length = length se.. 2018. 9. 18.
Python - Class class Person: salary = 0 def __init__(self, name): self.name = name def say_hi(self): print('Hello, my name is', self.name) def say_salary(cls): print('My salary is', cls.salary) cls.salary += 1 p1 = Person('Younghoi') p1.say_hi() Persin.say_salary() p2 = Person('Kim') p2.say_hi() Person.say_salary() init method 는 예상처럼 객체/인스턴스가 생성될때 호출되는 method이다. 재미있는것은 클래스변수와 객체 변수가 있다는 것이다. 클래스 변수인 salary는 서로.. 2018. 9. 7.
Python - 기본문법 # 쌍따옴표, 홑따옴표 차이는 없지만 홑따옴표를 기본으로 하자. print('hello world') # 따옴표 세 개 '''This is a multi-line string. This is the second line. "What's your name? ''' # 문자열 포맷팅 age = 20 name = 'yhkim' print('{0} was {1} years old when the wrote this book'.format(name, age)) # name + ' is ' str(age) + ' years old' 와 같이 작성하지 말고 format 을 이용하자. print('{0: .3f} , {1: .2f}'.format(1.0/3, 1/3)) print('{0:_^11}'.format('he.. 2018. 8. 27.
728x90
반응형
LIST