14일차 - Python

JeongChaeJin·2021년 4월 28일
0

함수

Swap

def swap(arg1, arg2):
    global a, b
    a, b = arg2, arg1

a = 5
b = 3
swap(a, b)
print(a, b)
  • 전역변수 선언은 global !

가변인수

  • 가변 인수는 위치 인수보다 뒤에 위치 시켜야 한다.
def a(x, *args):
	pass

지역변수 전역변수

def Mytestfunc():
    data = 20
    print(f"data : {data}")
    print(f"func inner : ", locals())

data = 80
Mytestfunc()
print('function outer : ', locals())
  • 전역변수와 지역변수 이름이 같으면 함수 내에서는 지역변수 우선이다.

Function List

def add_data(arg1, arg2):
    return arg1 + arg2

def sub_data(arg1, arg2):
    return arg1 - arg2

def multi_data(arg1, arg2):
    return arg1 * arg2

def div_add(arg1, arg2):
    return arg1 / arg2

func_list = [add_data, sub_data, multi_data, div_add]
a = 6
b = 3
print([ func(a, b) for func in func_list ])
  • 함수이름을 List로 하고 인자를 받아서 List화 시킬 수 있음.

Class

__add__

    def __add__(self, other):
        return [ self.list_data[idx] + other.list_data [idx] for idx in range(len(other.list_data))]
    • 키워드 연산을 정의할 수 있었다.

  • 연산자 오버로딩 기능이다 ! (매직 메서드)

Pandas

excel load

my_df = pd.read_excel('./seoul_pop.xlsx')

File Open/Close

f = open('test.txt', 'r')
for line in f:
    print(line, end='')
else:
    print()
print(f.tell())
f.close()

-readlines() 쓰면 list로 반환받음.

f.seek(0, 0)
  • 파일 포인터의 위치를 맨앞으로 지정할 수 있다.
  • 파일 포인터 위치는 f.tell()

pyinstaller

  • pyinstaller --noconsle ..py
  • pyinstaller --onefile ..py
  • 실행파일이 만들어진다.
profile
OnePunchLotto

0개의 댓글