본문 바로가기
Python/Web Scraping

[Python] #1.8 Keyworded Arguments (#코딩공부)

by 함께 공부해요 2020. 2. 4.

https://youtu.be/Z27sK7sNoSk

#1.7 💖은 니꼬쌤의 말씀!

I just wanted to say: Thank you for being here and watching our lectures.

See you on the next video!


<복습>

https://wook-2124.tistory.com/15

 

[Python] #1.6 Returns (#코딩공부)

https://youtu.be/hvK0kQhFCKw <복습> https://wook-2124.tistory.com/13 [Python] #1.5 Function Arguments https://youtu.be/k7fUI_X-yL8 <복습> https://wook-2124.tistory.com/12 [Python] #1.4 Creating a Yo..

wook-2124.tistory.com

print와 return의 차이에 대해 복습하고, Positional Arguments와 Keyworded Arguments의 차이에 대해 알아보자!


<준비물>

https://repl.it/

 

The world's leading online coding platform

Powerful and simple online compiler, IDE, interpreter, and REPL. Code, compile, and run code in 50+ programming languages: Clojure, Haskell, Kotlin (beta), QBasic, Forth, LOLCODE, BrainF, Emoticon, Bloop, Unlambda, JavaScript, CoffeeScript, Scheme, APL, Lu

repl.it

https://docs.python.org/3/library/

 

The Python Standard Library — Python 3.8.1 documentation

The Python Standard Library While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It also describes some of the opt

docs.python.org


1. Positional Arguments(의존적 인자)란?

Positional Arguments는 말 그대로 정해진 위치에 의존하는 인자를 말한다.

쉽게 설명하면 def plus(a, b)에서 a값과 b값은 정해져 있는 그 자리에 의해서만 출력이 된다는 말이다.

즉, result = plus(3, 7)에서 a=3, b=7이라고 자리에 의해서 결정이 된다.


2. Keyworded Arguments(키워드 인자)란?

반대로 Keyworded Arguments는 인자의 순서(a자리, b자리)를 신경 쓸 필요없이 인자의 이름만 신경쓰면 된다.

위 사진에 나온 것과 같이 def plus(a, b)에서 result = plus(b=4, a=3) 이렇게 a와 b를 반대로 놓고 이름만 신경써도 결과가 출력된 것을 알 수 있다.


3-1. say_hello(name, age) 일치시키기 1

Positional Arguments로 def say_hello(name, age)에 name, age처럼 순서대로 "Wook", 27을 적어줬다.

(+return은 str에 () 안씌워도 함수가 읽힌다.)

정확히 입력했지만 터미널에는 return "Hello name, you are age years old?" 라고 내가 적어둔 대로 출력됨을 알 수 있다. (아직까지는 name과 age 인자가 읽히지 않는 것!)


3-2. say_hello(name, age) 일치시키기 2

return함수 "Hello name, you are age years old?" 앞에 f를 썻다.

f는 format이고, 내가 원하는 각 argument(인자)에 {}를 씌워줘야 함수가 제대로 작동하는 것을 알 수 있다.


3-3 say_hello(name, age) 일치시키기 3

만약 이것을 2번같이 안하고 각 str마다 "___" 씌워 str으로 지정하고 argument(인자)를 +로 분리해서 구별하면 사진에서 보이는 것과 같이 작업도 번거로워지고 띄어쓰기도 맞지 않는 것을 알 수 있다... 상당히 번거로움!!!!


3-4. say_hello(name, age) 일치시키기 4

그리고 사진에서 보이는 것과 같이 스페이스바로 띄어쓰기까지 생각해야함.. 2번째 사진 방법이 최고!!!


4. keyworded Arguments 활용하기

반대로 Keyworded Arguments를 활용해서 2번째 방법으로 f"Hello {name}, you are {age} years old?"로 지정해둔 함수에 {age}인자 age=27, {name}인자 name="Wook"처럼 키워드화 해서 출력하니 정말 편리하다!!!


5. 배운 내용 활용하기!

def say_hello에 인자 name, age, where_from, fav_food 4개를 추가했다.

 

그리고 이걸 return값으로 호출하자면 f"Hello {name}, you are {age} years old?" f"And {where_from} is good to live?" f"So is this your {fav_food}??" 이 되는데,

 

여기서 주의해야할 점은 각 str 앞에는 꼭 f(format)을 입력해줘야 {}의 Keyworded Arguments가 인식이 된다는 것!

그리고 띄어쓰기 역시 잘 생각해서 적고, Keyworded Arguments를 활용해서 인자의 이름만 기억해서 출력하면 된다.


6. 마지막으로 다시 깔끔하게 정리!

배운 내용을 처음 활용했을 때 출력된 문장이 너무 어색해서 다시 적어봤다..!!

그리고 각 str 마다 f를 적어주는 것도 너무 혼잡해서 하나의 "___"(str)으로 묶고 f를 적은 뒤, 인자들을 {}으로 묶어줬다.

 

그 결과:

return f"Hello {name}, you are {age} years old? And you are from {where_from} right? So {fav_food} is your favorite food??"

처럼 내가 생각하기엔 깔끔하게 나왔다!!ㅋㅋㅋ 그리고 Keyworded Arguments를 활용해서 인자마다 =을 정해서 출력해주면 끝!!

 

정리도 끝!


※ 신종 코로나 바이러스 조심하세요!!!!

댓글