본문 바로가기

전체 글

(646)
[Python] 고급 - 리스트 및 튜플(2) 컨테이너 타입 자료형(Container : 서로다른 자료형[list, tuple, collections.deque], a = [3, 3.5, 'a'] # 서로 다른 자료형, 컨네이너 타입 자료형 Flat : 한 개의 자료형[str, bytes, bytearray, array.array, memoryview]) 한개의 자료형만 저장, 빠름, 자연어 처리, 숫자, 이산, 회계분석, 기상데이터 등 단일 형태의 연산 가변(list, bytearray, array.array, memoryview, deque) 불변(tuple, str, bytes) unpacking # Tuple Advanced # Unpacking # b, a = a, b print(divmod(100, 9)) print(divmod(*(100..
[Python] 고급 - 리스트 및 튜플(1) 컨테이너 타입 자료형(Container : 서로다른 자료형[list, tuple, collections.deque], a = [3, 3.5, 'a'] # 서로 다른 자료형, 컨네이너 타입 자료형 Flat : 한 개의 자료형[str, bytes, bytearray, array.array, memoryview]) 한개의 자료형만 저장, 빠름, 자연어 처리, 숫자, 이산, 회계분석, 기상데이터 등 단일 형태의 연산 가변(list, bytearray, array.array, memoryview, deque) 불변(tuple, str, bytes) # 지능형 리스트(Comprehending Lists) # Non Comprehending Lists chars = '+_)(*&^%$#@!~)' code_list1 ..
[Python] NamedTuple 일반적인 tuple과 namedtupel 사용방법 일반적인 tuple 사용시, 인덱스로 접근하므로 오류 가능성이 높고, 직관적이지 않다 # 네임드 튜플 사용 from collections import namedtuple from math import sqrt # 일반적인 튜플 사용 pt1 = (1.0, 5.0) pt2 = (2.5, 1.5) # 일반적인 튜플 계산, 인덱스로 접근가능 l_leng1 = sqrt((pt2[0] - pt1[0]) ** 2 + (pt2[1] - pt1[1]) ** 2) # 네임드 튜플 선언 Point = namedtuple('Point', 'x y') # 두 점 선언 pt3 = Point(1.0, 5.0) pt4 = Point(2.5, 1.5) # 네임드 튜플 계산, 키값으로 ..
[Python] Magic Method(2) 클래스 샘플2 class Vector(object): def __init__(self, *args): # function Doctring '''(init)Create a vector, example : v = Vector(5,10)''' if len(args) == 0: self._x, self._y = 0, 0 else: self._x, self._y = args # un-packing def __repr__(self): '''(repr)Returns the vector infomations''' return '(repr)Vector(%r, %r)' % (self._x, self._y) def __add__(self, other): '''(add)Returns the vector addition of ..
[Python] Magic Method(1) 클래스 예제 class Fruit: def __init__(self, name, price): self._name = name self._price = price def __str__(self): return '(str)Fruit Class Info : {} , {}'.format(self._name, self._price) def __ge__(self, x): print('Called >> __ge__ Method.') if self._price >= x._price: return True else: return False def __le__(self, x): print('Called >> __le__ Method.') if self._price = s2) print(s1 > __ge__ Method...
[Python] Class(3) - Class Method, Static Method Instance method, Class method, Static method # 클래스 선언 class Car(object): ''' Car Class Author : Me Date : Description : Class, Static Method, Class Method ''' # Class Variable(모든 인스턴스가 공유 가능) price_per_raise = 1.0 def __init__(self, company, details): self._company = company self._details = details def __str__(self): return 'str : {} - {}'.format(self._company, self._details) def __repr__(self):..
[Python] Class(2) - self, 매직 매소드 # 클래스 재 선언 class Car(): """ Car Class """ # 클래스 변수 car_count = 0 def __init__(self, company, details): self._company = company # 인스턴스 변수 self._details = details Car.car_count += 1 # 클래스 변수 def __str__(self): return 'str : {} - {}'.format(self._company, self._details) def __repr__(self): return 'repr : {} - {}'.format(self._company, self._details) def detail_info(self): print('Current Id : {}'.fo..
[Python] Class(1) - Basic print(car1) 과 print(car_list) 의 차이에 주의할 것 print(car1) - str 로 출력 print(car_list) - repr 로 출력 class Car(): def __init__(self, company, details): self._company = company self._details = details def __str__(self): # 매직 메소드, 간단한 사용자 레벨의 출력 return 'str : {} - {}'.format(self._company, self._details) def __repr__(self): # 매직 메소드, 엄격한 개발자 레벨의 출력 return 'repr : {} - {}'.format(self._company, self._detai..