본문 바로가기

파이썬

파이썬 컨벤션 이란

컨벤션이란 일종의 스타일 관례를 말한다

 

같음을 확인하기

#나쁜 코드
if attr == True:
	print 'True'

if attr == None:
	print 'none'

 

#좋은 코드

if attr: #값이 존재하는가
	print 'attr is truthy!'

if not attr: #값이 없는가
	print 'attr is falsey'
    
if attr is True: #값이 참인가
	print 'attr is true'
    
if attr is None: #값이 none인가
	print 'attr is none'

 

리스트 다루기

#4보다 큰수만 남기기
a = [3,4,5]
b = [i for i in a if a>4]

#lambda
b = filter(lambda x: x>4, a)

반응형