์ง๋ฌธ์ Top 100 Python Interview Questions You Must Prepare In 2021 - edureka!์ ์ฐธ๊ณ ํ์์ต๋๋ค.
- What is the difference between list and tuples in Python?
- What are the key features of Python?
- What type of language is python? Programming or scripting?
- Python an interpreted language. Explain.
- What is pep 8?
- How is memory managed in Python?
- What is namespace in Python?
- What is PYTHONPATH?
- What are python modules? Name some commonly used built-in modules in Python?
- What are local variables and global variables in Python?
- Is python case sensitive?
- What is type conversion in Python?
- How to install Python on Windows and set path variable?
- Is indentation required in python?
- What is the difference between Python Arrays and lists?
- What are functions in Python?
- What is
__init__? - What is a lambda function?
- What is self in Python?
- How does break, continue and pass work?
- What does
[::-1]do? - How can you randomize the items of a list in place in Python?
- Whatโs the difference between iterator and iterable?
- How can you generate random numbers in Python?
- What is the difference between range & xrange?
- How do you write comments in python?
- What is pickling and unpickling?
- What are the generators in python?
- How will you capitalize the first letter of string?
- How will you convert a string to all lowercase?
- How to comment multiple lines in python?
- What are docstrings in Python?
- What is the purpose of is, not and in operators?
- What is the usage of help() and dir() function in Python?
- Whenever Python exits, why isnโt all the memory de-allocated?
- What is a dictionary in Python?
- How can the ternary operators be used in python?
- What does this mean:
*args,**kwargs? And why would we use it? - What does len() do?
- Explain split(), sub(), subn() methods of โreโ module in Python.
- What are negative indexes and why are they used?
- What are Python packages?
- How can files be deleted in Python?
- What are the built-in types of python?
- What advantages do NumPy arrays offer over (nested) Python lists?
- How to add values to a python array?
- How to remove values to a python array?
- Does Python have OOps concepts?
- What is the difference between deep and shallow copy?
- How is Multithreading achieved in Python?
- What is the process of compilation and linking in python?
- What are Python libraries? Name a few of them.
- What is split used for?
- How to import modules in python?
- Explain Inheritance in Python with an example.
- How are classes created in Python?
- What is monkey patching in Python?
- Does python support multiple inheritance?
- What is Polymorphism in Python?
- Define encapsulation in Python?
- How do you do data abstraction in Python?
- Does python make use of access specifiers?
- How to create an empty class in Python?
- What does an object() do?
- What is map function in Python?
- Is python numpy better than lists?
- What is GIL in Python language?
- What makes the CPython different from Python?
- What are Decorators in Python?
- What is object interning?
- What is @classmethod, @staticmethod, @property?
๋ฆฌ์คํธ๋ mutable(๋ณ๊ฒฝ ๊ฐ๋ฅ), ํํ์ immutable(๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ)์ด๋ผ๋ ํน์ง์ ๊ฐ์ง๊ณ ์๋ค. ๋ฐ๋ผ์ ๋ฆฌ์คํธ๋ ์ ์ธ ํ์๋ ๊ฐ์ ๋ํ ๋ณ๊ฒฝ, ์ญ์ ๊ฐ ๊ฐ๋ฅํ์ง๋ง, ํํ์ ์ ์ธ ํ์ ๊ฐ์ ๋ณ๊ฒฝํ๊ฑฐ๋ ์ญ์ ํ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ค. ๋ํ ๋ฆฌ์คํธ๋ ํํ๋ณด๋ค ๋๋ฆฌ๋ค๋ ๋จ์ ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ํ๋์ ํํ/๋ฆฌ์คํธ์ ๋ค๋ฅธ ํ์
์ ๊ฐ์ ํจ๊ป ์ ์ฅํ ์ ์๋ค๋ ๊ณตํต์ ์ด ์๋ค. ๋ฆฌ์คํธ๋ ๋๊ดํธ [ ]๋ฅผ, ํํ์ ์๊ดํธ ( )๋ฅผ ์ฌ์ฉํด์ ๋ํ๋ธ๋ค.
ํ์ด์ฌ์ด ์ฃผ์ ํน์ง์ ์๋์ ๊ฐ๋ค.
- ์ธํฐํ๋ฆฌํฐ ์ธ์ด(Interpreter Language)
- ํ์ด์ฌ์ ์ธํฐํ๋ฆฌํฐ ์ธ์ด์ด๋ฏ๋ก, ์คํํ๊ธฐ ์ ์ ์ปดํ์ผ์ ํ ํ์๊ฐ ์๋ค.
- ์์ธํ ๋ด์ฉ์ Python an interpreted language. Explain. ์ฐธ๊ณ
- ๋์ ํ์ดํ(Dynamic Typing)
- ํ์ด์ฌ์ ์คํ์๊ฐ์ ์๋ฃํ์ ๊ฒ์ฌํ๋ฏ๋ก, ์ ์ธํ ๋ ๋ณ์ ์ ํ(ex.int, double, ...)์ ๋ช ์ํ ํ์๊ฐ ์๋ค.
typing์ด๋ ํ๋ก๊ทธ๋จ ๋ด์์ ๋ณ์์ ๋ฐ์ดํฐ ํ์ ์ ์ ํ๋ ๊ฒ์ ๋งํ๋ค. ๋ฐ์ดํฐ ํ์ ์ง์ (assign)์ ์ ์ ๋๋ ๋์ ํ์ดํ์ผ๋ก ๋ถ๋ฅ๋๋๋ฐ, ํ๋ก๊ทธ๋จ ์ปดํ์ผ ์์ ๋ณ์์ ํ์ ์ ์ฒดํฌํ๋ C, C++๊ณผ ๊ฐ์ ์ธ์ด๋ ์ ์ ํ์ (static typed) ์ธ์ด๋ผ๊ณ ํ๊ณ , ํ๋ก๊ทธ๋จ ์คํ ์์ ํ์ ์ ์ฒดํฌํ๋ python์ ๋์ ํ์ (dynamic typed) ์ธ์ด์ด๋ค.
- ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP)
- ํ์ด์ฌ์ ํด๋์ค์ ๊ตฌ์ฑ ๋ฐ ์์์ ํจ๊ป ์ ์ํ ์ ์๋ค๋ ์ ์์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๋งค์ฐ ์ ํฉํ๋ค.
- ์ผ๊ธ๊ฐ์ฒด(First-class citizen)
- ํ์ด์ฌ์์ ํจ์์ ํด๋์ค๋ ์ผ๊ธ ๊ฐ์ฒด์ด๋ค. ์ผ๊ธ๊ฐ์ฒด๋ ๋ณ์๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์์ ๋ด์ ์ ์๊ณ , ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ์ด ๊ฐ๋ฅํ๋ฉฐ, ๋ฆฌํด๊ฐ์ผ๋ก ์ฌ์ฉ๋ ์ ์๋ค๋ ํน์ง์ ๊ฐ์ง๊ณ ์๋ค.
์ด ์ธ ํน์ง
- ํ์ด์ฌ์ ๋ค์ฌ์ฐ๊ธฐ(indentation) ์ ๊ฐ๊ฒฐํ๊ณ ์ฌ์ด ๋ฌธ๋ฒ์ ํตํด ๋น ๋ฅด๊ฒ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค๋ ์ฅ์ ์ ๊ฐ์ง๊ณ ์๋ค.
- ๋ณ์, ์ธ์(argument)๋ฅผ ๋ฏธ๋ฆฌ ์ ์ธํ์ง ์์๋ ์๋์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ํ ๋น๋์ด ํธ๋ฆฌํ๋ค.
- ํจ์(function) ๋๋ ๋ชจ๋(module) ์ถ๊ฐ๊ฐ ์ฉ์ดํ์ฌ ํ์ฅ์ฑ๊ณผ ์ด์์ฑ์ด ์ข๋ค.
- ํ์ด์ฌ์ ์ธํฐํ๋ฆฌํฐ๋ก ๋์ํ๋ ์คํฌ๋ฆฝํธ ์ธ์ด์ด๋ฏ๋ก ๋ค๋ฅธ ์ปดํ์ผ ์ธ์ด์ ๋นํด ๋ค์ ๋๋ฆฌ๋ค.
- ์ปดํ์ผ๋ฌ๊ฐ ์ฝ๋๋ฅผ ๊ธฐ๊ณ์ด๋ก ๋ฒ์ญํด์ ์คํ๊ฐ๋ฅ ํ์ผ์ ๋ง๋๋ ๊ฒ์ ๋นํด, ์ธํฐํ๋ฆฌํฐ๋ ์ฝ๋๋ฅผ ํ์ค์ฉ ์คํ์๊ฐ๋ง๋ค ๋ฒ์ญํด์ ์คํํ๊ธฐ ๋๋ฌธ์ด๋ค.
- Python ์์ํ๊ธฐ - crystalcube
- ํ์ด์ฌ ๋์ ํ์ดํ๊ณผ ์บ์คํ - ์ค์ค๋ก ๋ฐฐ์ฐ๋ ์ฝ๋ฉ
- python ๋ฆฌ์คํธ, ํํ, ๋์ ๋๋ฆฌ ๋น๊ต - bskyvision
- Python ์ผ๊ธ๊ฐ์ฒด(FIRST-CLASS CITIZEN)- ํ์ฐฌ๊ธฐ
- ์ธํฐํ๋ฆฌํฐ ์ธ์ด์ ์ปดํ์ผ ์ธ์ด์ ์ฐจ์ด - jhkang-dev
ํ์ด์ฌ์ ์ ํํ๊ฒ๋, ์คํฌ๋ฆฝํธ ์ธ์ด์ด๋ค. ๋ชจ๋ ์คํฌ๋ฆฝํธ ์ธ์ด๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก ๋ณผ ์ ์์ผ๋, ๋ชจ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์คํฌ๋ฆฝํธ ์ธ์ด๋ก ๋ถ๋ฅ๋๋ ๊ฒ์ ์๋๋ค. ๋ฐ๋ผ์ ํ์ด์ฌ์ ์คํฌ๋ฆฝํธ ์ธ์ด์ด์, ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ค. ๊ทธ๋ฌ๋ ์ฌ๋๋ค์ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ์ ํ์ด์ฌ์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ชฉ์ ์ผ๋ก ๋ถ๋ฅํ๊ณ , ํ๋ก๊ทธ๋๋ฐ ๋ชฉ์ ์ผ๋ก ๋ง์ด ์ฌ์ฉํ๋ค.
๐ก ์คํฌ๋ฆฝํ (scripting/Scripting Language)
์คํฌ๋ฆฝํธ ์ธ์ด๋ ์ปดํ์ผ์ด ํ์์์ด ์คํ๋ ์ ์๋ ๋ช ๋ น์ด์ ์งํฉ์ด๋ค. ์คํฌ๋ฆฝํธ ์ธ์ด๋ ์ธํฐํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ๋๋ฐ, ์ธํฐํ๋ฆฌํฐ๋ ์ปดํ์ผ ๊ณผ์ ์ด ํ์ํ์ง ์์ผ๋ฉฐ, ์์ค์ฝ๋๋ก ๋ถํฐ ๋ฐ๋ก ๋ช ๋ น์ด๋ฅผ ํด์ํ ์ ์๋ค.
์ธํฐํ๋ฆฌํฐ๋ ๊ณ ๊ธ ์ธ์ด๋ก ์์ฑ๋ ์์์ฝ๋ ๋ช ๋ น์ด๋ค์ ํ๋ฒ์ ํ ์ค์ฉ ์ฝ์ด๋ค์ฌ์ ์คํํ๋ ํ๋ก๊ทธ๋จ์ด๋ค. ์ธํฐํ๋ฆฌํฐ ์ธ์ด๋ ์คํ์๊ฐ(runtime) ์ ์ ๊ธฐ๊ณ ๋ ๋ฒจ ์ฝ๋(machine-level code)๋ฅผ ๋ง๋๋ ์ปดํ์ผ ์ธ์ด์ ๋ค๋ฅด๊ฒ ์์ค์ฝ๋๋ฅผ ๋ฐ๋ก ์คํํ๋ ์ธ์ด์ด๋ฉฐ, ํ์ด์ฌ์ ์ธํฐํ๋ฆฌํฐ ์ธ์ด์ ํด๋นํ๋ค.
์ธํฐํ๋ฆฌํฐ ์ธ์ด๋ ์คํฌ๋ฆฝํธ ์ธ์ด์ ๋์ผํ ์๋ฏธ์ด๋ค. ์คํฌ๋ฆฝํ /์คํฌ๋ฆฝํธ ์ธ์ด์ ๋ํ ์ง๋ฌธ๊ณผ ๋ต๋ณ์ What type of language is python? Programming or scripting?์ ์ฐธ๊ณ ํ๋ค.
PEP(Python Enhancement Proposal)๋ Python ์ฝ๋๋ฅผ ํฌ๋งทํ๋ ๋ฐฉ๋ฒ์ ์ง์ ํ๋ ๊ท์น ์งํฉ์ด๋ค. ๋ค๋ฅธ ์ฌ๋๊ณผ ์ํํ๊ฒ ํ์
ํ๋ ค๋ฉด ๊ณตํต๋ ์คํ์ผ ๊ณต์ ๊ฐ ํ์ํ๋ฉฐ, ์ผ๊ด์ฑ ์๋ ์คํ์ผ์ ๋์ค์ ์์ ํ๊ธฐ๋ ์ฝ๋ค. PEP8์ ํ์ด์ฌ ์ฝ๋๋ฅผ ์ด๋ป๊ฒ ๊ตฌ์ฑํ ์ง ์๋ ค์ฃผ๋ ์คํ์ผ ๊ฐ์ด๋๋ก์์ ์ญํ ์ ํ๋ค. Python formatting tool์๋ black, flake8, autopep8, yamf ๋ฑ์ด ์๋ค.
PEP8 ์คํ์ผ ๊ฐ์ด๋ ์์
- whitespace
- ํ ์ค์ ๋ฌธ์ ๊ธธ์ด๊ฐ 79์ ์ดํ์ฌ์ผ ํ๋ค.
- ํจ์์ ํด๋์ค๋ ๋น ์ค ๋๊ฐ๋ก ๊ตฌ๋ถํ๋ค.
- naming
- ํจ์, ๋ณ์, ์์ฑ :
lowercase_underscore - ๋ณดํธ(protected) ์ธ์คํด์ค ์์ฑ :
_leading_underscore - ๋น๊ณต๊ฐ(private) ์ธ์คํด์ค ์์ฑ :
__double_leading_undersocre
- ํจ์, ๋ณ์, ์์ฑ :
Python์ ๋ชจ๋ ๊ฒ์ ๊ฐ์ฒด๋ก ๊ด๋ฆฌํ๋ค. ๊ฐ์ฒด๊ฐ ๋์ด์ ํ์ํ์ง ์์ผ๋ฉด ํ์ด์ฌ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์๊ฐ ์๋์ผ๋ก ๊ฐ์ฒด์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์ํ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ฏ๋ก, ํ์ด์ฌ์ ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ๋ฐฉ์์ ์ฌ์ฉํ๋ค๊ณ ๋งํ ์ ์๋ค. ํ(heap)์ ๋์ ํ ๋น์ ๊ตฌํํ๋๋ฐ ์ฌ์ฉ๋๋ค. ํ์ ์ฌ์ฉํ์ฌ ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ฉด, ํ์ํ์ง ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋น์ฐ๊ณ ์ฌ์ฌ์ฉํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค. ๋ชจ๋ ํ์ด์ฌ ๊ฐ์ฒด ๋๋ ์๋ฃ๊ตฌ์กฐ๋ python private heap ๊ณต๊ฐ์์ ๊ด๋ฆฌ๋๋ฉฐ, ํ๋ก๊ทธ๋๋จธ๋ ์ด ๊ณต๊ฐ์ ์ ๊ทผํ ์ ์๊ณ , ๋์ ํ์ด์ฌ ์ธํฐํ๋ฆฌํฐ๊ฐ ๋์ ํด์ ๊ด๋ฆฌํ๋ค.
๋ ์์ธํ๋ณด๊ธฐ
ํ์ด์ฌ ๊ฐ์ฒด์ ๋ํ ํ ๊ณต๊ฐ ํ ๋น์ ๋ด๋นํ๋ ๊ฒ์ ํ์ด์ฌ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์(Python Memory Manager) ๋ผ๊ณ ๋ถ๋ฅธ๋ค. Python ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์์๋ ๊ฐ์ฒด๋ณ ํ ๋น์๊ฐ์๊ธฐ ๋๋ฌธ์ int, string ๋ฑ๊ณผ ๊ฐ์ ํน์ ๊ฐ์ฒด์ ๋ํด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ช ํํ๊ฒ ํ ๋น ํ ์ ์๋ค. ๋ํ, ์ฌ์ฉ๋์ง ์๋ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌํ์ฉํ๊ณ ํ ๊ณต๊ฐ์์ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ๋ด์ฅ Garbage Collector(GC) ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- [๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ] ํ์ด์ฌ(Python)์์ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌํ๊ธฐ - DEVLOG/๊ฐ๋ฐ์ผ๊ธฐ
- ํ์ด์ฌ ๋ฐํ์๊ณผ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ - muchogusto
namespace๋ ์ด๋ฆ ์ง์ ์ถฉ๋(naming conflicts)์ ํผํ๊ธฐ ์ํด ์ด๋ฆ์ด ๊ณ ์ ํ์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ด๋ฆ ์ง์ ์์คํ (naming system)์ด๋ค. ๋ค์์คํ์ด์ค(namespace, ์ด๋ฆ๊ณต๊ฐ)๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ํน์ ํ ๊ฐ์ฒด(Object)๋ฅผ ์ด๋ฆ(Name)์ ๋ฐ๋ผ ๊ตฌ๋ถํ ์ ์๋ ๋ฒ์๋ฅผ ์๋ฏธํ๋ค. ํ์ด์ฌ ๋ด๋ถ์ ๋ชจ๋ ๊ฒ์ ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋๋ฉฐ ์ด๋ค ๊ฐ๊ฐ์ ํน์ ์ด๋ฆ๊ณผ์ ๋งคํ ๊ด๊ณ๋ฅผ ๊ฐ๊ฒ ๋๋๋ฐ ์ด ๋งคํ์ ํฌํจํ๊ณ ์๋ ๊ณต๊ฐ์ ๋ค์์คํ์ด์ค๋ผ๊ณ ํ๋ค.
๋ค์์คํ์ด์ค๊ฐ ํ์ํ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค. ํ๋ก๊ทธ๋๋ฐ์ ์ํํ๋ค๋ณด๋ฉด ๋ชจ๋ ๋ณ์ ์ด๋ฆ๊ณผ ํจ์ ์ด๋ฆ์ ์ ํ๋ ๊ฒ์ด ์ค์ํ๋ฐ ์ด๋ค ๋ชจ๋๋ฅผ ๊ฒน์น์ง ์๊ฒ ์ ํ๋ ๊ฒ์ ์ฌ์ค์ ๋ถ๊ฐ๋ฅํ๋ค. ๋ฐ๋ผ์ ํ๋ก๊ทธ๋๋ฐ์ธ์ด์์๋ ๋ค์์คํ์ด์ค๋ผ๋ ๊ฐ๋ ์ ๋์ ํ์ฌ, ํน์ ํ ํ๋์ ์ด๋ฆ์ด ํต์ฉ๋ ์ ์๋ ๋ฒ์๋ฅผ ์ ํํ๋ค. ์ฆ, ์์๋ ๋ค์์คํ์ด์ค๊ฐ ๋ค๋ฅด๋ค๋ฉด ๊ฐ์ ์ด๋ฆ์ด ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
ํ์ด์ฌ ๋ค์์คํ์ด์ค์ ํน์ง
- ๋ค์์คํ์ด์ค๋ ๋์ ๋๋ฆฌ ํํ๋ก ๊ตฌํ๋๋ค.
- ๋ชจ๋ ์ด๋ฆ ์์ฒด๋ ๋ฌธ์์ด๋ก ๋์ด์๊ณ ๊ฐ๊ฐ์ ํด๋น ๋ค์์คํ์ด์ค์ ๋ฒ์์์ ์ค์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
- ์ด๋ฆ๊ณผ ์ค์ ๊ฐ์ฒด ์ฌ์ด์ ๋งคํ์ ๊ฐ๋ณ์ (Mutable)์ด๋ฏ๋ก ๋ฐํ์๋์ ์๋ก์ด ์ด๋ฆ์ด ์ถ๊ฐ๋ ์ ์๋ค.
- ๋นํธ์ธ ๋ค์์คํ์ด์ค๋ ํจ๋ถ๋ก ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ์ ์๋ค.
ํ์ด์ฌ ๋ค์์คํ์ด์ค์ 3๊ฐ์ง ๋ถ๋ฅ
๋นํธ์ธ ๋ค์์คํ์ด์ค(build-in namespace): ๊ธฐ๋ณธ ๋ด์ฅ ํจ์ ๋ฐ ๊ธฐ๋ณธ ์์ธ๋ค์ ์ด๋ฆ๋ค์ด ์์๋๋ค. ํ์ด์ฌ์ผ๋ก ์์ฑ๋ ๋ชจ๋ ์ฝ๋ ๋ฒ์๊ฐ ํฌํจ๋๋ค.์ ์ญ ๋ค์์คํ์ด์ค(global namespace): ๋ชจ๋๋ณ๋ก ์กด์ฌํ๋ฉฐ, ๋ชจ๋ ์ ์ฒด์์ ํต์ฉ๋ ์ ์๋ ์ด๋ฆ๋ค์ด ์์๋๋ค.์ง์ญ ๋ค์์คํ์ด์ค(local namespace): ํจ์ ๋ฐ ๋ฉ์๋ ๋ณ๋ก ์กด์ฌํ๋ฉฐ, ํจ์ ๋ด์ ์ง์ญ ๋ณ์๋ค์ ์ด๋ฆ๋ค์ด ์์๋๋ค.
๋ชจ๋์ importํ ๋ ์ฌ์ฉ๋๋ ํ๊ฒฝ๋ณ์์ด๋ค. ๋ชจ๋์ importํ ๋๋ง๋ค PYTONPATH๋ฅผ ์กฐํํ์ฌ ๊ฐ์ ธ์จ ๋ชจ๋์ด ๋๋ ํ ๋ฆฌ์ ์๋์ง ํ์ธํ๋ค. ์ธํฐํ๋ฆฌํฐ๋ ์ด๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๋ํ ๋ชจ๋์ ๊ฒฐ์ ํ๋ค.
PYTHONPATH ํ๊ฒฝ ๋ณ์์ ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํ๋ฉด, ํ์ด์ฌ์ ์ด ๊ฒฝ๋ก๋ค์ sys.path์ ์ถ๊ฐํ๋ค. ์ด๋ฅผ ํตํด ํ์ด์ฌ ์ฝ๋ ๋ด๋ถ์์ ๋ฟ๋ง ์๋๋ผ ํ์ด์ฌ ์ฝ๋ ๋ฐ์์๋ sys.path๋ฅผ ์กฐ์ํ ์ ์๋ค. PYTHONPATH์๋ sys.path์ ์ถ๊ฐํ ์ฌ๋ฌ ๊ฒฝ๋ก๋ค์ด ๋ค์ด๊ฐ๋ค. ๋ฆฌ๋
์ค์์๋ /foo:/bar์ฒ๋ผ :๋ก ๋ ๊ฒฝ๋ก๋ฅผ ๊ตฌ๋ถํ๊ณ , ์๋์ฐ์์๋ /foo;/bar์ฒ๋ผ ;๋ก ๋ ๊ฒฝ๋ก๋ฅผ ๊ตฌ๋ถํ๋ค. ์ด์ธ์๋ sys.path์๋ ํ์ด์ฌ์ ํฌํจ๋ ์ฌ๋ฌ ๋ด์ฅ ๋ชจ๋ ๋ฑ์ ํ์ํ๊ธฐ ์ํ ๊ธฐ๋ณธ ๊ฒฝ๋ก๊ฐ ๋ค์ด๊ฐ๋ค.
sys.path์ ์์
import๋ sys.path ๋ฆฌ์คํธ์ ๋ค์ด์๋ ๊ฒฝ๋ก๋ค์ ํ์ํ๋ฉฐ ๋ถ๋ฌ์ฌ ํ์ด์ฌ ํ์ผ์ ์ฐพ๋๋ค. ๋ฆฌ์คํธ์ ๋ค์ด์๋ ๋งจ ์ฒ์ ๊ฒฝ๋ก๋ถํฐ ํ์์ ์์ํ์ฌ, ํน์ ๊ฒฝ๋ก์์ ๋ถ๋ฌ์ฌ ํ์ผ์ ์ฐพ์๋ค๋ฉด ๋จ์ ๊ฒฝ๋ก๋ฅผ ๋ ์ฐพ์๋ณด์ง ์๊ณ ํ์์ ์ค์งํ๋ค.
sys.path์ ๊ธฐ๋ณธ๊ฐ์ ์๋์ ์์๋๋ก ์ถ๊ฐ๋๋ค.
.pyํ์ผ์ด ์ํ ๋๋ ํฐ๋ฆฌ์ ์ ๋ ๊ฒฝ๋ก- PYTHONPATH ํ๊ฒฝ ๋ณ์
- ๊ธฐํ ๊ธฐ๋ณธ ๊ฒฝ๋ก
์๋์ ์ฝ๋๋ฅผ ํตํด์ sys.path๋ฅผ ์ง์ ํ์ธํ ์ ์๋ค.
import sys
print(sys.path)- sys.path, PYTHONPATH: ํ์ด์ฌ ํ์ผ ํ์ ๊ฒฝ๋ก - ๋ฐฉ์ฑ๋ฒ (Bang Seongbeom)
- Python sys module, path ์ ๋ฆฌ - ์ผ๋ฅ๋ ์ผ๋ฅ์์ ๋ถํฐ
๋ชจ๋์ด๋ Python ์ฝ๋๋ฅผ ํฌํจํ๋ ํ์ผ๋ก์จ, ํจ์๋ ๋ณ์ ๋๋ ํด๋์ค๋ฅผ ๋ชจ์ ๋์ ํ์ผ์ด๋ค. ๋ชจ๋์ ๋ค๋ฅธ ํ์ด์ฌ ํ๋ก๊ทธ๋จ์์ ๋ถ๋ฌ์ ์ฌ์ฉํ ์ ์๊ฒ๋ ๋ง๋ ํ์ด์ฌ ํ์ผ์ด๋ผ๊ณ ๋ ํ ์ ์๋ค. ์คํ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ํฌํจํ๋, ํ์ด์ฌ ํ์ฅ์ .py๋ก ๋ง๋ ํ์ด์ฌ ํ์ผ์ ๋ชจ๋ ๋ชจ๋์ด๋ค. ๋ชจ๋์ ์ฌ์ฉํ๋ฉด, ๋ค๋ฅธ ์ฝ๋์ ์ ์ฉํ๊ธฐ๊ฐ ์ฌ์์ง๋ฏ๋ก ์ด์์ฑ์ด ์ข์์ง๋ค.
์์ฃผ ์ฌ์ฉ๋๋ ๋นํธ์ธ ๋ชจ๋(built-in module)์ ์์๋ ๋ค์๊ณผ ๊ฐ๋ค.
ossysmathrandomdatetimeJSON
**์ ์ญ ๋ณ์(Global Variable)**๋ ํจ์ ์ธ๋ถ ๋๋ ์ ์ญ ๊ณต๊ฐ์ ์ ์ธ๋ ๋ณ์์ด๋ค. ํ๋ก๊ทธ๋จ์ ๋ชจ๋ ํจ์์์ ์ ์ญ๋ณ์์ ์ ๊ทผํ ์ ์๋ค. (Whenever Python exits, why isnโt all the memory de-allocated? ์ฐธ๊ณ )
**๋ก์ปฌ ๋ณ์(Local Variable)**๋ ํจ์ ๋ด๋ถ์ ์ ์ธ๋ ๋ณ์๋ฅผ ๋งํ๋ค. ๋ก์ปฌ ๋ณ์๋ ์ ์ญ ๊ณต๊ฐ์ด ์๋ ๋ก์ปฌ ๊ณต๊ฐ์ ์๋ค.
a=2
def add():
b=3
c=a+b
print(c)
add()
# ์ถ๋ ฅ: 5
# global var: a
# local var: b, cadd() ํจ์์ ์ธ๋ถ์์ add() ํจ์์ ๋ก์ปฌ ๋ณ์์ ์ก์ธ์คํ๋ ค๊ณ ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
ํ์ด์ฌ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ ์ธ์ด์ด๋ค. ์๋ฅผ๋ค์ด, a์ A๋ ๋ค๋ฅธ ๋ณ์์ด๋ค.
type conversion์ ํ์ ์บ์คํ (type casting)๊ณผ ๋์ผํ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ฉฐ, ์ด๋ ์ด๋ค ๋ฐ์ดํฐ ํ์ ์ ๋ค๋ฅธ ๋ฐ์ดํฐ ํ์ ์ผ๋ก ๋ณํํ๋ ๊ฒ์ ๋งํ๋ค.
ํ์ ์บ์คํ ํจ์์ ์ข ๋ฅ
int(): ์ ์ํ์ผ๋ก ๋ณํํ๋ค.float(): ์ค์ํ์ผ๋ก ๋ณํํ๋ค.ord(): ๋ฌธ์ํ์ ์ ์ํ์ผ๋ก ๋ณํํ๋ค.hex(): ์ ์ํ์ 10์ง์๋ก ๋ณํํ๋ค.oct(): ์ ์ํ์ 8์ง์๋ก ๋ณํํ๋ค.tuple(): ํํํ์ผ๋ก ๋ณํํ๋ค.set(): set์ผ๋ก ๋ณํํ๋ค.list(): list๋ก ๋ณํํ๋ค.dict(): (key,value) ์์๋ก ์ด๋ค์ง ํํ์ ๋์ ๋๋ฆฌํ์ผ๋ก ๋ณํํ๋ค.str(): ์ ์ํ์ ๋ฌธ์ํ์ผ๋ก ๋ณํํ๋ค.complex(real, image): ์ค์๋ฅผ ๋ณต์์๋ก ๋ณํํ๋ค.
Windows์ Python์ ์ค์นํ๋ ค๋ฉด ๋ค์ ๋จ๊ณ๋ฅผ ๊ฑฐ์ณ์ผํ๋ค.
- ๋งํฌ์์ python์ ์ค์นํ๋ค.
- PC์ ๋ค์ด๋ก๋ ๋ฐ์ python์ ์ค์นํ๋ฉด์,
Add Python 3.6 to PATH์ ์ฒดํฌํ๊ณ , ์๋ด์ ๋ฐ๋ผ ์ค์นํ๋ฉฐ python์ ์ค์นํ ์์น๋ฅผ ์ ์ฅํด๋๋ค. ์์คํ > ์์คํ ์ ๋ณด > ๊ณ ๊ธ ์์คํ ์ค์ > ํ๊ฒฝ๋ณ์์ผ๋ก ์ด๋ํ์ฌ ์์คํ ๋ณ์๋ฅผ ํธ์งํ์ฌ 2๋ฒ์์ ์ ์ฅํด๋ python.exe ์คํํ์ผ์ด ์๋ ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
Python์ Indentation(๋ค์ฌ์ฐ๊ธฐ)์ด ํ์ํ๋ค. ํ์ด์ฌ์ {}์ ์ฌ์ฉํ์ฌ ์์ญ์ ์ง์ ํ์ง ์๊ณ , ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ธ๋ก์ ์ง์ ํ๊ธฐ ๋๋ฌธ์ ํ์ด์ฌ์์ ๋ค์ฌ์ฐ๊ธฐ๋ ๋ฌธ๋ฒ์ ์ธ ๊ฐ์ ์ฌํญ์ด๋ค. if, for, class, def ๋ฑ์ ๋ชจ๋ ์ฝ๋๋ ๋ค์ฌ์ฐ๊ธฐ ๋ธ๋ก ๋ด์์ ์ง์ ๋๋ค. ๋ค์ฌ์ฐ๊ธฐ์ ๋ฐฉ๋ฒ์ 1์นธ, 2์นธ, 4์นธ, ํญ ๋ฑ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ์์ด ์๋ค. ์ผ๋ฐ์ ์ผ๋ก ํ์ด์ฌ์ ๋ค ๊ฐ์ ๊ณต๋ฐฑ ๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ์ํํ๋ค.
์ฝ๋๊ฐ ์ ํํ๊ฒ ๋ค์ฌ์ฐ์ฌ์ง์ง ์์ผ๋ฉด ์คํ๋์ง ์๊ณ ์ค๋ฅ๋ ๋ฐ์ํ๋ค. ์ค์ํ ๊ฒ์ ๊ฐ์ ๋ธ๋ก ๋ด์์๋ ๋ค์ฌ์ฐ๊ธฐ ์นธ ์๊ฐ ๊ฐ์์ผํ๋ค๋ ๊ฒ์ด๋ค. ๋ค์ฌ์ฐ๊ธฐ ๊ท์น ์๋ฐ์์๋ IndentationError: unexpected indent ์๋ฌ๋ฅผ ์ถ๋ ฅํ๋ค.
Python์์๋ array๊ณผ list๊ฐ ๋์ผํ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค. ์ฐจ์ด์ ์, ๋ฐฐ์ด์ ๋จ์ผ ๋ฐ์ดํฐ ํ์
์์๋ง ํฌํจํ ์ ์๋ ๋ฐ๋ฉด, ๋ฆฌ์คํธ์๋ ๋ค์ํ ํ์
์ ์์๋ค์ด ๋ค์ด๊ฐ ์ ์๋ค๋ ๊ฒ์ด๋ค. array์ ์ ์ธ ๋ฐฉ๋ฒ์ arrayName = array(type, [Values])์ฒ๋ผ ์๋ฃํ์ ์ ํ๊ณ , ์ง์ ํ/๋์ผํ ์๋ฃํ๋ง์ ๋ฃ์ ์ ์๋๋ก ๋์ด์๋ค. list์ ๋ณ์์ []๋ก ์ฌ๋ฌ ํ์
์ ๋ณ์๋ฅผ ๋ฌถ์ด์ ์ ์ธํ ์ ์๋ค.
array์์ ์ฌ์ฉํ ์ ์๋ ํ์ ์ ์๋์ ๊ฐ๋ค.
import array as arr
My_Array=arr.array('i',[1,2,3,4])
My_list=[1,'abc',1.20]
print(My_Array)
print(My_list)
# Output: array(โiโ, [1, 2, 3, 4]) [1, โabcโ, 1.2]ํจ์๋ ํธ์ถ๋ ๋๋ง ์คํ๋๋ ์ฝ๋ ๋ธ๋ก์ด๋ค. Python ํจ์๋ฅผ ์ ์ํ๊ธฐ ์ํด def ํค์๋๊ฐ ์ฌ์ฉ๋๋ค. ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ ํจ์๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ๋ฉด, ๋๊ฐ์ ์ฝ๋๋ฅผ ์ฌ๋ฌ๋ฒ ๋ฐ๋ณตํ์ฌ ์ฐ์ง ์์๋ ๋๊ณ , ํ๋ก๊ทธ๋จ์ ํ๋ฆ์ ์ฝ๊ฒ ํ์ ํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
def new_func():
print("Hi, Welcome to Edureka")
new_func(); # ํจ์ ํธ์ถ
# Output: Hi, Welcome to Edureka__init__๋ ํ์ด์ฌ์์ ํน๋ณํ๊ฒ ์ฝ์๋ ๋ฉ์๋ ๊ฐ์ด๋ฐ ํ๋๋ก, ์ด๊ธฐํ ๋ฉ์๋ ํน์ ์์ฑ์๋ผ๊ณ ๋ ํ๋ค. ์ด ๋ฉ์๋๋ ํด๋์ค์ ์ ๊ฐ์ฒด/์ธ์คํด์ค๊ฐ ์์ฑ๋ ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๊ธฐ ์ํด ์๋์ผ๋ก ํธ์ถ๋๋ฉฐ, ๊ทธ ๊ฐ์ฒด๊ฐ ๊ฐ๊ฒ ๋ ์ฌ๋ฌ ๊ฐ์ง ์ฑ์ง์ ์ ํด์ค๋ค. ๋ชจ๋ ํด๋์ค์๋ __init__ ๋ฉ์๋๊ฐ ์๋ค.
class Employee:
def __init__(self, name, age,salary):
self.name = name
self.age = age
self.salary = 20000
E1 = Employee("XYZ", 23, 20000)
# E1์ Employee ํด๋์ค์ ๊ฐ์ฒด
# __init__ ๋ E1์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํจ
print(E1.name)
print(E1.age)
print(E1.salary)
'''
์ถ๋ ฅ:
XYZ
23
20000
'''๐ก
__init__.py์ ๋ฌด์์ธ๊ฐ?
python 3.3 ์ดํ ๋ฒ์ ์์, package importํ๊ธฐ ์ํด์ ์ฌ์ฉ๋๋ ๊ท์น์ด๋ค. 3.3 ์ดํ์ ๋ฒ์ ์์๋ ์ด ์ ์ฝ์ฌํญ์ด ํด์ ๋์๋ค.__init__.py๋ python ํ๋ก๊ทธ๋จ์ด ๋๋ ํ ๋ฆฌ๋ฅผ ์ฒ์ ๊ฐ์ ธ์ฌ ๋ ์๋์ผ๋ก ์คํ๋๋ ํจํค์ง ์ด๊ธฐํ ํ์ผ ์ญํ ์ ํ๊ณ , ๋ชจ๋์ ๋ค์์คํ์ด์ค ์ด๊ธฐํ ์ญํ ์ ํ๋ค.
- 7.5. ํน๋ณํ ๋ฉ์๋๋ค - ์์ด๋ณด๋ฅผ ์ํ Python
- ํจํค์ง init.py ํ์ผ๋ค - ๊ทธ๊ฒ์ผ๋ก ๋ง๋ฏธ์์,
์ต๋ช
ํจ์(์ด๋ฆ์ด ์๋ ํจ์)๋ฅผ ๋๋ค ํจ์๋ผ๊ณ ํ๋ค. ๋๋ค ํจ์๋ def ํค์๋๋ฅผ ํตํด์ ํจ์๋ฅผ ์์ฑํ๋ ๋ฆฌํฐ๋ด ํ๊ธฐ๋ฒ์ ๋ฑ ํ ์ค์ ์ฝ๋๋ก ํํํ ์ ์๊ฒ ํด์ฃผ๋ฉฐ, lambda ์ธ์ : ํํ์์ ํ์์ผ๋ก ํํํ๋ค. ๋๋คํจ์๋ ๊ฒฐ๊ณผ ๋ถ๋ถ์ return ํค์๋ ์์ด ์๋์ผ๋ก returnํ๋ค. ๋๋คํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๊ณ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ์ฝ๋๋ค๋ ์ฅ์ ์ด ์๋ค. ๊ทธ๋ฌ๋ ํจ์์ ์ด๋ฆ์ด ์๊ณ , ์ ์ฅ๋ ๋ณ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ค์ ์ฝ๋๋ฅผ ์ ์ด์ฃผ๊ฑฐ๋, ๋๋คํจ์๋ฅผ ๋ณ์์ ๋ด์์ฃผ์ด์ผํ๋ค. ๋ฐ๋ผ์, ์ฌ์ฌ์ฉํ ์ด์ ๊ฐ ์๋ค๋ฉด lambda ํจ์๋ฅผ ์์ฑํ์ฌ ๋๊ฒจ์ฃผ๋ ํธ์ด ์ข๋ค.
๋๋คํจ์์ ํํ๋ฒ์ ๊ทธ๋ฆผ์ผ๋ก ํํํ๋ฉด ์๋์ ๊ฐ๋ค.
a = lambda x, y : x + y
print(a(5, 6))
# Output: 11- 3.5 ๋๋ค(lambda) - ์์ด๋ณด๋ฅผ ์ํ Python - WikiDocs
- 4) ๋๋คํจ์(์ต๋ช ํจ์) - ์ ๋๋ก ํ์ด์ฌ - WikiDocs
class MyClass:
def method(self):
return 'instance method', self
obj = MyClass
print(obj.method())
# >> ('instance method', <__main__.MyClass object at 0x7f10aa8e68b0>)์ฐ์ self๊ฐ ์ด๋์์ ์ฐ์ด๋์ง ์์์ผ ํ๋ค. self๋ ์ธ์คํด์ค ๋ฉ์๋(instance method)์ ์ฒซ ๋ฒ์งธ ์ธ์์ด๋ค. ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋, ํ์ด์ฌ์ self์ ์ธ์คํด์ค๋ฅผ ๋ฃ๊ณ ์ด ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ์ฌ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ์คํํ ์ ์๊ฒ ๋๋ค.
- self ์ดํดํ๊ธฐ - ํ์ด์ฌ์ผ๋ก ๋ฐฐ์ฐ๋ ์๊ณ ๋ฆฌ์ฆ ํธ๋ ์ด๋ฉ
- Python's Instance, Class, and Static Methods Demystified - Real Python
- Why must โselfโ be used explicitly in method definitions and calls? - Python Documentation
break๋ ๊ฐ์ฅ ๊ฐ๊น์ด for๋ฌธ์ด๋ while๋ฌธ์ ๋ฃจํ์์ ๋น ์ ธ๋๊ฐ๋๋ก ํ๋ค.
for i in range(10):
if i == 5:
break
print(i, end=' ')
# >> 0 1 2 3 4continue๋ ์ด๋ฒ ์ดํฐ๋ ์ด์
(iteration)์ ๊ฑด๋๋ฐ๊ณ ๋ค์ ์ดํฐ๋ ์ด์
์ ์ด์ด๋๊ฐ๋๋ก ํ๋ค.
for i in range(10):
if i == 5:
continue
print(i, end=' ')
# >> 0 1 2 3 4 6 7 8 9pass๋ ๋ฌธ๋ฒ์ ์ผ๋ก ํ์ํ์ง๋ง, ์๋ฌด ๊ฒ๋ ํ์ง ์๊ฒ ํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค. ์ฃผ๋ก ํจ์๋ ํด๋์ค์ ๊ตฌ์กฐ๋ถํฐ ์ธ์ฐ๊ณ ๋์ค์ ๊ตฌํ์ ํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
class MyClass:
def not_implemented_method(self):
pass- ๋ฃจํ์ break ์ continue ๋ฌธ, ๊ทธ๋ฆฌ๊ณ else ์ - Python Documentation
- pass ๋ฌธ - Python Documentation
ํ์ด์ฌ ์ํ์ค ์๋ฃํ์ ๊ฐ์ด ์ฐ์์ ์ผ๋ก ์ด์ด์ง ์๋ฃํ์ผ๋ก, ๋ฆฌ์คํธ, ํํ, range, ๋ฌธ์์ด์ด ์๋ค. ์ํ์ค ์๋ฃํ์ ์ํ์ค ๊ฐ์ฒด์ ์ผ๋ถ๋ฅผ ์๋ผ๋ผ ์ ์๋ **์ฌ๋ผ์ด์ฑ(slicing)**์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ธ ์ ์๋ค. ์ฌ๋ผ์ด์ฑ์ seq[start:end:step]์ฒ๋ผ ์ธ ์ ์์ผ๋ฉฐ, start๋ ์์ ์ธ๋ฑ์ค, end๋ ๋ ์ธ๋ฑ์ค(๋ฒ์์ ํฌํจํ์ง๋ ์์), step์ ์ธ๋ฑ์ค ์ฆ๊ฐํญ์ ๋งํ๋ค. step์ด ์์์ด๋ฉด ์ฆ๊ฐํ๊ณ , ์์์ด๋ฉด ๊ฐ์ํ๋ค.
๋ค์ ๋์์ seq[::-1]์ start์ end๋ ์์ ์ธ๋ฑ์ค์ ๋ ์ธ๋ฑ์ค๋ฅผ ์๋ตํ์๋๋ฐ, ์ด๋ด ๊ฒฝ์ฐ ์ ์ฒด ์ํ์ค๋ฅผ ๊ฐ์ ธ์ค๋ฉฐ, ์ฆ๊ฐํญ์ด -1์ด๋ฏ๋ก end-1๋ถํฐ ์์ํด start์์ผ๋ก ์์๋ฅผ ๊ฐ์ ธ์จ๋ค. ์ฆ, seq[::-1]์ ์ํ์ค๋ฅผ ์ญ์ (reverse)์ํจ๋ค.
- ์ํ์ค ์๋ฃํ ํ์ฉํ๊ธฐ - ํ์ด์ฌ ์ฝ๋ฉ ๋์ฅ
- ์ฌ๋ผ์ด์ค ์ฌ์ฉํ๊ธฐ - ํ์ด์ฌ ์ฝ๋ฉ ๋์ฅ
random ๋ชจ๋์ shuffle ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๊ตฌํํ ์ ์๋ค. random.shuffle์ ์ํ์ค ๊ฐ์ฒด์ ์์๋ฅผ ์์๋ก ์์ด์ ํด๋น ์ํ์ค๋ฅผ ๋ฐํํ๋ค.
import random
random.seed(2021) # ์๋ ๊ณ ์
lst = list(range(10))
print(lst) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(lst)
print(lst) # [2, 7, 9, 3, 0, 5, 1, 4, 8, 6]- Shuffle a list, string, tuple in Python (random.shuffle, sample) - note.nkmk.me
- random - Python Documentation
lst = [1, 2] # iterable ๊ฐ์ฒด : ๋ฆฌ์คํธ
lst_iter = iter(lst) # iterator ๊ฐ์ฒด
print(next(lst_iter)) # 1
print(next(lst_iter)) # 2
print(next(lst_iter)) # StopIteration ์์ธ ๋ฐ์iterable ๊ฐ์ฒด๋ iter ํจ์์ ์ธ์๋ก ์ ๋ฌ ๊ฐ๋ฅํ, ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค. ์๋ฅผ ๋ค๋ฉด, ๋ฆฌ์คํธ(list), ๋์
๋๋ฆฌ(dictionary), ์งํฉ(set), ๋ฌธ์์ด(string) ๋ฑ์ด ์๋ค.
iterable ๊ฐ์ฒด๋ฅผ iter ํจ์์ ์ธ์๋ก ๋ฃ์ผ๋ฉด iterable ๊ฐ์ฒด๋ฅผ ์ํํ ์ ์๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋๋ฐ, ์ด๊ฒ์ด iterator ๊ฐ์ฒด์ด๋ค. iterator ๊ฐ์ฒด๋ฅผ next ํจ์์ ์ธ์๋ก ์ฃผ๋ฉด iterable ๊ฐ์ฒด์ ์์์ ๊ฐ์ ์ฐจ๋ก๋๋ก ๋ฐํํ๋ค. ๋ง์ฝ iterable ๊ฐ์ฒด๋ฅผ ๋ชจ๋ ์ํํ๋ค๋ฉด, StopIteration ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค. ๋ง์ฝ ๋ค์ ์ํ๋ฅผ ํ๊ณ ์ถ๋ค๋ฉด iter ํจ์๋ก ์๋ก์ด iterator ๊ฐ์ฒด๋ฅผ ์์ฑํด์ฃผ๋ฉด ๋๋ค.
- ๐ ์ ๋๋ ์ดํฐ(Generator) - ์ฝ๋ฉํ๋ํญ๊ท์ ์ ์ฅ์
- Iterable ๊ณผ Iterator - ํ์ด์ฌ - ๊ธฐ๋ณธ์ ๊ฐ๊ณ ๋ฆ์!
random ๋ชจ๋๋ก ๊ฐ๋จํ ์์ฑํ ์ ์๋ค. ํธ์๋ฅผ ์ํด ๋ค์๊ณผ ๊ฐ์ด random ๋ชจ๋์ importํ๊ณ ์๋๊ฐ์ 2021๋ก ๊ณ ์ ํ์.
import random
random.seed(2021)- 0๊ณผ 1์ฌ์ด์ ์์์ ์๋ฅผ ์์ฑํ๊ณ ์ถ์ ๊ฒฝ์ฐ
print(random.random()) # 0.8363375023320742- ํน์ ๋ฒ์ ๋ด์ ์์์ ์ ์๋ฅผ ์์ฑํ๊ณ ์ถ์ ๊ฒฝ์ฐ
print(random.randint(0, 10)) # 10- ํน์ ๋ฒ์ ๋ด์ n๊ฐ์ ์ ์๋ฅผ ์์ฑํ๊ณ ์ถ์ ๊ฒฝ์ฐ
n = 5
lst = range(1, 101)
print(random.sample(lst, 5)) # [70, 36, 32, 82, 5]ํ์ด์ฌ2์์๋
range์xrange๋ชจ๋ ์กด์ฌํ์ง๋ง, ํ์ด์ฌ3๋ถํฐ๋range๊ฐ ๋ด๋ถ์ ์ผ๋กxrange๋ก ๋์ํ๋๋ก ๋ฐ๋์ด์range๋ง ์กด์ฌํ๋ค. ๊ทธ๋ฌ๋ฏ๋ก ํ์ด์ฌ2๋ฅผ ๊ธฐ์ค์ผ๋กrange์xrange๋ฅผ ์ค๋ช ํ๋ค.
range ๊ฐ์ฒด๋ ์
๋ ฅ์ผ๋ก ๋ฐ์ ์ ์ ๋ฒ์์ ๊ฐ์ ์์๋ก ๊ฐ๋ ๋ฆฌ์คํธ๋ฅผ ๋งํ๋ค. ๊ทธ๋ฌ๋ฏ๋ก range(3)๊ณผ [0, 1, 2]๋ ์์ ํ ๋์ผํ๋ค.
# python2
r = range(5)
print(r) # [0, 1, 2, 3, 4]
print(type(r)) # <type 'list'>xrange๋ ์ ๋๋ ์ดํฐ ๊ฐ์ฒด๋ก, ์ค์ง ๋ฃจํ๋ฅผ ๋๋๋ง ํด๋น ๋ฒ์์ ์ ์๋ฅผ ํ๋์ฉ ๋ฐํํ๋ค. ์ ๋๋ ์ดํฐ์ ๊ดํ ์ค๋ช
์ ์ฌ๊ธฐ์์!
#python2
r = xrange(5)
print(r) # xrange(5)
print(type(r)) # <type 'xrange'>
for i in r:
print i,
# >> 0 1 2 3 4- python range() ์ xrange() ์ฐจ์ด - ใ ใ ใ ใท
- [python] range, xrange ์ฐจ์ด - ์ฝ๋ฉ์ฅ์ด
- range() vs xrange() in Python - GeeksforGeeks
#์ ์ฌ์ฉํ์ฌ ์ฃผ์์ ๋ฌ ์ ์๋ค.
# this is my comment๋ฐ์ดํ๋ฅผ ์ด์ฉํ ์ฃผ์์ Docstring ํ์์ผ๋ก ์์ธํ ๋ด์ฉ์ #32๋ฅผ ์ฐธ๊ณ !
์ฐ์ ์ง๋ ฌํ(Serialization)์ ์ญ ์ง๋ ฌํ(Deserialization)์ ๊ฐ๋
์ ์์์ผ ํ๋ค. ์ง๋ ฌํ๋ ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํธ ์คํธ๋ฆผ(byte stream)์ผ๋ก ๋ณํํ์ฌ ๋์คํฌ์ ์ ์ฅํ๊ฑฐ๋ ๋คํธ์ํฌ๋ก ๋ณด๋ผ ์ ์๋๋ก ๋ง๋ค์ด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. ๋ฐ๋๋ก ๋ฐ์ดํธ ์คํธ๋ฆผ์ ํ์ด์ฌ ๊ฐ์ฒด๋ก ๋ณํํ๋ ๊ฒ์ ์ญ ์ง๋ ฌํ๋ผ๊ณ ํ๋ค.
pickle ๋ชจ๋์ ํ์ด์ฌ ๊ฐ์ฒด์ ์ง๋ ฌํ์ ์ญ ์ง๋ ฌํ๋ฅผ ์ํํ๋ ๋ชจ๋์ด๋ค. ์ด ๋ ํ์ด์ฌ ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํํ ๋๋ฅผ pickling์ด๋ผ๊ณ ํ๋ฉฐ, ๋ฐ์ดํธ ์คํธ๋ฆผ์ ์ญ ์ง๋ ฌํํ ๋๋ฅผ unpickling์ด๋ผ๊ณ ํ๋ค.
- pickle โ ํ์ด์ฌ ๊ฐ์ฒด ์ง๋ ฌํ - Python Documentation
- The Python pickle Module: How to Persist Objects in Python - Real Python
์ ๋๋ ์ดํฐ(Generator)๋ Iterator ๊ฐ์ฒด๋ฅผ ๊ฐ๋จํ ๋ง๋ค ์ ์๋ ํจ์๋ฅผ ๋งํ๋ค. ์ ๋๋ ์ดํฐ๋ ๋ค์๊ณผ ๊ฐ์ด 1) yield๋ฌธ๊ณผ ํจ์, 2) ํํ์ ํํ๋ก ๋ง๋ค ์ ์๋ค.
๋ฐฉ๋ฒ 1. yield๋ฌธ๊ณผ ํจ์
- ์ ๋๋ ์ดํฐ ํจ์ ์ ์
def generator_list(value):
for i in range(value):
# ๊ฐ์ ๋ฐํํ๊ณ ์ฌ๊ธฐ๋ฅผ ๊ธฐ์ต
yield i- ์ ๋๋ ์ดํฐ ๊ฐ์ฒด ์์ฑ ๋ฐ next ํจ์๋ก ํธ์ถ
gen = generator_list(2)
print(next(gen)) # 0
print(next(gen)) # 1
print(next(gen)) # StopIteration ์๋ฌ ๋ฐ์๋ฐฉ๋ฒ 2. ํํ๋ฌธ
value = 2
gen = (i for i in range(value))
print(next(gen)) # 0
print(next(gen)) # 1
print(next(gen)) # StopIteration ์๋ฌ ๋ฐ์๊ทธ๋ผ ์ ๋ฆฌ์คํธ ๋์ ์ ๋๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ ๊น? ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฆฌ์คํธ์ ํฌ๊ธฐ๋งํผ ๋ฉ๋ชจ๋ฆฌ์ ๊ณต๊ฐ์ด ํ ๋น๋๋ค. ๋ฐ๋ฉด ์ ๋๋ ์ดํฐ๋ ๋ง๊ทธ๋๋ก next ํจ์๋ก ํธ์ถ๋ ๋ ๊ฐ์ ์์ฑํ๊ณ ํด๋น ๊ฐ๋ง ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฐ๋ค! ์ฆ, ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํ ์ ์๋ค. ์์ ๋ฐ์ดํฐ๋ผ๋ฉด ์๊ด์์ง๋ง ํฐ ๋ฐ์ดํฐ์์๋ ์ ๋๋ ์ดํฐ ์ฌ์ฉ์ด ํ์์ด๋ค.
- ๐ ์ ๋๋ ์ดํฐ(Generator) - ์ฝ๋ฉํ๋ํญ๊ท์ ์ ์ฅ์
- How to Use Generators and yield in Python - Real Python
๋ฌธ์์ด ๋ฉ์๋ capitalize๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
string = "boostcamp ai tech"
print(string.capitalize()) # Boostcamp ai tech์์ธํ ๋ฌธ์์ด ๋ฉ์๋๋ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ !
๋ฌธ์์ด ๋ฉ์๋ lower์ ์ฌ์ฉํ๋ฉด ๋๋ค.
string = "BOOSTCAMP AI TECH"
print(string.lower()) # boostcamp ai tech#์ ์ฌ๋ฌ ์ค ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ์ค์ ์ฃผ์์ ๋ฌ ์ ์๋ค.
# this is my comment
# I am commenting multiple lines
# - boostcamp ai tech team 4๋ฐ์ดํ๋ฅผ ์ด์ฉํ ์ฃผ์์ Docstring ํ์์ผ๋ก ์์ธํ ๋ด์ฉ์ #32๋ฅผ ์ฐธ๊ณ !
docstrings์ ์ฃผ์์ ์๋์ง๋ง, ์ฌ์ฉ์์๊ฒ ์ฝ๋์ ๋ํ ์ค๋ช
์ ์ ์ด๋์ ๋ฌธ์(documentation)์ด๋ค. docstrings๋ __doc__ ์์ฑ์ด๋ help() ๋ด์ฅ ํจ์๋ก ์ ๊ทผํ ์ ์๋ค. docstrings๋ ์์ ๋ฐ์ดํ(') ํน์ ํฐ ๋ฐ์ดํ(") 3๊ฐ๋ก ์์ฑํ ์ ์๋ค.
def mult(a, b):
"""
Returns the product of a and b
- a(float): any real number
- b(float): any real number
"""
return a*bprint(help(mult))
print(mult.__doc__)๐ก Comments(์ฃผ์) vs Dosctrings
comments์ docstrings์ ๊ฐ๊ฐ#,"""์ ์ด๋ค๋ ์ ์์ ๋ค๋ฅด์ง๋ง ๊ฐ์ฅ ํฐ ์ฐจ์ด๋ ์ฝ๋ ๋์์ด๋ค. comments๋ ๊ฐ๋ฐ์ ์ํด ๋๋ฃ ํน์ ๋์ค์ ์ฝ๋๋ฅผ ์ฝ์ ๋์๊ฒ ๋จ๊ฒจ๋๋ ๊ฒ์ด๊ณ docstrings๋ ์ด ์ฝ๋๋ฅผ ์ฌ์ฉํ ์ฌ์ฉ์๋ค์ด ์ดํดํ๊ธฐ ์ฝ๋๋ก ๋จ๊ฒจ๋๋ ๊ฒ์ด๋ค.
is๋ ๊ฐ์ฒด ๋น๊ต ์ฐ์ฐ์(identity operator)๋ก ๋ ๋ณ์๊ฐ ์ฐธ์กฐํ๋ ๊ฐ์ฒด์ id๊ฐ ๊ฐ์ ๊ฒฝ์ฐ True๋ฅผ ๋ฐํํ๋ค. ๋ณดํต ๋ ๋ณ์๊ฐ ์ฐธ์กฐํ๋ ๊ฐ์ฒด๊ฐ ๋์ผํ ๊ฐ์ฒด์ธ์ง ํ์ธํ ๋ ์ฌ์ฉํ๋ค.
a = [1, 2, 3]
b = a
c = a.copy()
print(a is b) # True
print(a is c) # Falsenot์ ๋จํญ ๋
ผ๋ฆฌ ์ฐ์ฐ์(logical operator)๋ก ๋ค์ ์ค๋ boolean ๊ฐ์ ๋ค์ง๋๋ค. ๋ค์ ์ค๋ ๊ฐ์ด True์ด๋ฉด False๋ฅผ, False์ด๋ฉด True๋ฅผ ๋ฐํํ๋ค.
print(not True) # False
print(not False) # Truein์ ๋ฉค๋ฒ ์ฐ์ฐ์(membership operator)๋ก, ์์ a์ ์ํ์ค b๊ฐ ์๋ ์ง๋ฅผ ํ์ธํ๊ณ ์ถ์ ๋ a in b๋ก ํํํ๋ฉฐ ๋ง์ฝ a๊ฐ b ์์ ์๋ค๋ฉด True๋ฅผ, ์์ผ๋ฉด False๋ฅผ ๋ฐํํ๋ค.
b = "abc"
print("a" in b) # True
print("z" in b) # Falsehelp()๋ docstrings๋ฅผ ์์ฑํ์๋ค๋ฉด ํด๋น docstrings๋ฅผ ์ถ๋ ฅํ๋ค. docstrings์๋ ํด๋์ค, ๋ฉ์๋์ ์ฌ์ฉ๋ฒ์ ๊ดํ ๋ด์ฉ์ด ๋ด๊ฒจ์์ผ๋ฏ๋ก ํด๋น ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ฌ์ฉ์์๊ฒ ๋งค์ฐ ์ ์ฉํ๋ค. docstrings์ ๋ํ ๋ด์ฉ์ #31 ์ฐธ๊ณ !
dir()์ ์ธ์๋ก ๋ฃ์ ๊ฐ์ฒด์ ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ๊ณ ๊ทธ๊ฒ์ ์์๋ก ๊ฐ๋ ์ ๋ ฌ๋ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ค. dir์ ์ฌ์ฉํ ๊ฐ์ฒด์ ๋ฉ์๋์ ์์ฑ์ ๋ํ ์ ๋ณด๋ฅผ ์ป๊ณ ์ถ์ ๋ ์ ์ฉํ๋ค. ๋ค๋ง ์ธ์๊ฐ ์๋ค๋ฉด ํ์ฌ ์ง์ญ ์ค์ฝํ์์ ์ ์๋ ํจ์์ ๋ณ์๋ค์ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ค.
def func(x):
return x
a = 3
print(dir(a)) # ๊ฐ์ฒด a์ ๋ํ ์์ฑ, ๋ฉ์๋
print(dir(func)) # ํจ์ func์ ๋ํ ์์ฑ, ๋ฉ์๋
print(dir()) # ์ง์ญ ์ค์ฝํ์ ์ ์๋ a์ func- ๋ด์ฅ ํจ์: help() - Python Documentation
- ๋ด์ฅ ํจ์: dir() - Python Documentation
- 10 Python built-in functions you should know
๋ค๋ฅธ ๊ฐ์ฒด๋ ์ ์ญ ๋ค์์คํ์ด์ค์์ ์ฐธ์กฐ๋๋ ๊ฐ์ฒด๋ฅผ ์ํ ์ฐธ์กฐํ๋ ํ์ด์ฌ ๋ชจ๋์ ํญ์ ํด์ ๋์ง๋ ์๋๋ค. ๋ํ C ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ฝํ ๋ฉ๋ชจ๋ฆฌ์ ํด๋น ๋ถ๋ถ์ ํด์ ํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค. ๊ทธ๋ฌ๋ฏ๋ก ํ์ด์ฌ ์ข ๋ฃ ์, ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํด์ ๋์ง๋ ์๋๋ค.
๐ก ์ํ ์ฐธ์กฐ(Circular Reference)
๋ ๊ฐ์ฒด๊ฐ ์๋ก ์ฐธ์กฐํ๋ ๊ฒฝ์ฐ๋ฅผ ๋งํ๋ค.
๐ก ์ ์ญ ๋ค์์คํ์ด์ค(Global Namespace)
๋ค์์คํ์ด์ค(namespace)๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ํน์ ๊ฐ์ฒด๋ฅผ ์ด๋ฆ์ ๋ฐ๋ผ ๊ตฌ๋ถํ ์ ์๋ ๋ฒ์๋ฅผ ์๋ฏธํ๋ค. ์ ์ญ ๋ค์์คํ์ด์ค๋ importํ ๋ชจ๋๋ค์ ์ด๋ฆ์ ํฌํจํ๋ฉฐ, ์คํฌ๋ฆฝํธ๊ฐ ๋๋ ๋๊น์ง ์ง์๋๋ค.
- Releasing memory in Python - Net-informations.com
- Whenever you exit Python, is all memory de-allocated? - BYTES
- Circular References in Python - hearsaysocial
- [Python] ๋ค์์คํ์ด์ค ๊ฐ๋ ์ ๋ฆฌ - Hyungcheol Noh's Blog
- ๋ค์์คํ์ด์ค - ์ ๋๋ก ํ์ด์ฌ
๋์ ๋๋ฆฌ๋ key๊ฐ๊ณผ ๊ทธ์ ๋์ํ๋ value๊ฐ์ ์ป์ ์ ์๋ ์ปฌ๋ ์ ์ ๋งํ๋ค. ๋์ ๋๋ฆฌ๋ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์จ ์์๊ฐ ์๊ด์ด ์๊ณ , ์ธ๋ฑ์ฑ์ด ๋์ด ์์ด key๊ฐ์ผ๋ก ์์์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ(= value) ์์ ์ด ๊ฐ๋ฅํ๋ค. ํ์ง๋ง, key๊ฐ์ ๊ณ ์ ๊ฐ์ด๋ฏ๋ก key๊ฐ ์ค๋ณต์ ๋ถ๊ฐ๋ฅํ๋ค. ์ฃผ๋ก ์์ฒด์ ์ผ๋ก ๋ง๋ key๊ฐ์ผ๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ณ ์ถ์ ๋ ๋์ ๋๋ฆฌ ์ปฌ๋ ์ ์ ์ฌ์ฉํ๋ค.
๋์ ๋๋ฆฌ์ ๋ป์ ์ฌ์ ์ด๋ค. ์ํ ์ฌ์ ์์ ๊ฐ ์๋จ์ด(ex. beautiful)์ ๋์ํ๋ ๋จ์ด(ex. ์๋ฆ๋ค์ด)๊ฐ ๋์ค๋ ๊ฒ์ฒ๋ผ, ์๋จ์ด๊ฐ key๊ฐ์ด๊ณ ๊ทธ์ ๋์ํ๋ ๋จ์ด๋ฅผ value๊ฐ์ผ๋ก ๋ณผ ์ ์๋ค.
ํน์ง1 : ๋์ ๋๋ฆฌ๋ {, }๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธํ๋ฉฐ { key1 : value1, key2 : value2, ... } ๋ก ์์๋ฅผ ๋ํ๋ธ๋ค.
- key๊ฐ์ผ๋ก ๋ณํ์ง ์๋ ๊ฐ์ ์ฌ์ฉํ๊ณ , value๊ฐ์ผ๋ก ๋ณํ๋ ๊ฐ๊ณผ ๋ณํ์ง ์๋ ๊ฐ ๋ ๋ค ์ฌ์ฉํ ์ ์๋ค.
- key๊ฐ์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ์ฌ์ฉํ๋ฉด, ๊ฐ์ด ๋ณํ ๊ฐ๋ฅ์ฑ์ด ์๊ธฐ ๋๋ฌธ์ ์ธํฐํ๋ฆฌํฐ์์ type error๋ฅผ ๋ฐ์์ํจ๋ค.
ex1 = {'name':'Groot', 'lover':'penguin', 'feature':'really cute'}
# ๋์
๋๋ฆฌ ์์ฑ์๋ก ์งํฉ์ ์์ฑํ๋ ๊ฒฝ์ฐ
ex2 = dict(name='Groot', lover='penguin', feature='really cute')
# key๊ฐ์ ๋ณํ์ง ์๋ ๊ฐ, value๊ฐ์ ๋ณํ์ง ์๋ ๊ฐ๊ณผ ๋ณํ๋ ๊ฐ ๋ ๋ค ๊ฐ๋ฅ
ex3 = {[10, 3]:'birthday'} # type error!ํน์ง2 : ๋์ ๋๋ฆฌ๋ ๋์ ๋๋ฆฌ ์(key : value)์ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ์ ์๋ค.
- ์ถ๊ฐ : dict_ex[ ์๋ก์ด key๊ฐ ] = ๊ทธ์ ๋์ํ๋ value๊ฐ์ผ๋ก ์ถ๊ฐํ ์ ์๋ค.
- ์ ๊ฑฐ : del ํค์๋๋ฅผ ์ด์ฉํด ํน์ ์์ ์ ๊ฑฐํ ์ ์๋ค.
- ๋์ ๋๋ฆฌ ์์ del ํค์๋๋ฅผ ์ฐ๋ฉด ๋์ ๋๋ฆฌ๊ฐ ์์ ํ ์ ๊ฑฐ๋๋ค.
ex = {'Kevin':180, 'Anna':165, 'Penelope':175}
# ์๋ก์ด ๋์
๋๋ฆฌ ์ ์ถ๊ฐ
ex['Groot'] = 100
print(ex) # {'Kevin': 180, 'Anna': 165, 'Penelope': 175, 'Groot': 100}
# del ํค์๋๋ก ๋์
๋๋ฆฌ ์ ์ ๊ฑฐ
del ex['Penelope']
print(ex) # {'Kevin': 180, 'Anna': 165, 'Groot': 100}
# del ํค์๋๋ก ๋์
๋๋ฆฌ ์์ ์ ๊ฑฐ
del ex
print(ex) # name error!ํน์ง3 : ๋์ ๋๋ฆฌ์ key๊ฐ์ ์ค๋ณต๋ ์ ์๋ค.
- key๊ฐ์ ๊ณ ์ ๊ฐ์ด๋ฏ๋ก 2๊ฐ ์ด์์ key๊ฐ์ด ์กด์ฌํ ์ ์๋ค.
- key๊ฐ์ ํด๋นํ๋ value๊ฐ์ ์ด๋ค ๊ฑธ ๋ถ๋ฌ์ผํ ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ.
- key๊ฐ์ด ์ค๋ณต๋ ๊ฒฝ์ฐ ํ๋๋ฅผ ์ ์ธํ ๋๋จธ์ง ๊ฒ๋ค์ด ๋ชจ๋ ๋ฌด์๋๋ค.
- key๊ฐ์ ๋์ ํ ์ต๊ทผ์ value๊ฐ์ ๋ถ๋ฌ์จ๋ค.
ex = {'name': 'Groot', 'lover': 'Penguin', 'feature': 'cute', 'feature': 'handsome'}
print(ex) # {'name': 'Groot', 'lover': 'Penguin', 'feature': 'handsome'}ternary operators(์ผํญ ์ฐ์ฐ์)๋ ์กฐ๊ฑด๋ฌธ์ ํ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ฐ์ฐ์์ด๋ฉฐ [true_value] if [condition] else [false_value]์ ํํ๋ก ํํ๋๋ค.
a = 123
print("a is 123" if a==123 else "a is not 123") # a is 123
a = 456
print("a is 123" if a==123 else "a is not 123") # a is not 123*args๋ ํจ์์ ์ ๋ฌ๋๋ argument์ ์๋ฅผ ์ ์ ์๊ฑฐ๋, list๋ tuple์ argument๋ค์ ํจ์์ ์ ๋ฌํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค.
ํ์ด์ฌ์์๋ ์ด๋์๋ถํฐ ์ด๋๊น์ง *args์ ๋ด์์ผ ํ ์ง ์ ์ ์๊ธฐ ๋๋ฌธ์, ์ผ๋ฐ ๋ณ์๋ฅผ ์์ ๋๊ณ ๊ทธ ๋ค์ *args๋ฅผ ์ง์ ํด ์ฃผ์ด์ผ ํ๋ค.
def name(*args):
print(args)
name("์๋ฆฌ", "ํญ๊ท", "ํ์ค", "์๋")
# output: ('์๋ฆฌ', 'ํญ๊ท', 'ํ์ค', '์๋')**kwargs๋ ํจ์์ ์ ๋ฌ๋๋ keyword argument์ ์๋ฅผ ๋ชจ๋ฅด๊ฑฐ๋, dictionary์ keyword argument๋ค์ ํจ์์ ์ ๋ฌํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค.
*args์ **kwargs๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ *args๋ฅผ **kwargs๋ณด๋ค ์์ ๋์ด์ผ ํ๋ค.
def name(**kwargs):
print(kwargs)
name(sally="์๋ฆฌ", penguin="ํญ๊ท", heath="ํ์ค", adc="์๋")
# output: {'sally': '์๋ฆฌ', 'penguin': 'ํญ๊ท', 'heath': 'ํ์ค', 'adc': '์๋'}์ผ๋ฐ ๋ณ์, *args, **kwargs๋ฅผ ๋ชจ๋ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ์์๋ฅผ ๋ฐ๋ฅธ๋ค.
def function_name(์ผ๋ฐ๋ณ์, *args, **kwargs)len() ํจ์๋ object์ ๊ธธ์ด(item์ ์)๋ฅผ return ํ๋ค.
argument๋ก๋ sequence(string, bytes, tuple, list, range, ...), collection(dictionary, set, frozenset, ...)์ ๋ฐ๋๋ค.
stg = "ai-tech-interview"
len(stg) #17
ex_list = ["ai", "tech", "interview"]
len(ex_list) # 3ํ์ด์ฌ์์ ์ ๊ทํํ์์ ์ฌ์ฉํ๊ธฐ ์ํด โreโ ๋ชจ๋์ ์ฌ์ฉํ๋ค. ๋ฌธ์์ด ์์ ์ ์ํด Python์ โreโ ๋ชจ๋์ 3 ๊ฐ์ง ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
re.split(pattern, string, maxplit=0): pattern์ ๊ตฌ๋ถ์๋ก string์ ๋ถ๋ฆฌํ์ฌ list๋ก ๋ฐํํ๋ค.
re.split('<[^<>]*>', '<html> Wow <head> header </head> <body> Hey </body> </html>')
# output: ['', ' Wow ', ' header ', ' ', ' Hey ', ' ', '']re.sub(pattern, repl, string, count=0): string์์ pattern๊ณผ ์ผ์นํ๋ ๋ถ๋ถ์ ๋ํ์ฌ repl๋ก ๊ต์ฒดํ์ฌ ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
re.sub('\d{4}', 'XXXX', '010-1234-5678')
# output: '010-XXXX-XXXX're.subn(pattern, repl, string, count=0): sub์ ๋์ผํ๋, ๊ฒฐ๊ณผ๋ก (๊ฒฐ๊ณผ๋ฌธ์์ด, ๋งค์นญํ์)๋ฅผ ํํ๋ก ๋ฐํํ๋ค.
re.subn('\d{4}', 'XXXX', '010-1234-5678')
# output: ('010-XXXX-XXXX', 2)- ํ์ด์ฌ โ ์ ๊ท์ํํ์(Regular Expression) ๋ชจ๋ - devanix
- ํ์ด์ฌ ์ ๊ทํํ์(re) ์ฌ์ฉ๋ฒ - 05. ์ฃผ์, ์นํ, ๋ถ๋ฆฌ - YW & YY's Python, Machine & Deep Learning
์ธ๋ฑ์ค
- ์ํ์ค ๊ฐ์ฒด์
[](๋๊ดํธ)๋ฅผ ๋ถ์ฌ ์ฌ์ฉ - ์ํ์ค ๊ฐ์ฒด์ ์ธ๋ฑ์ค๋ ํญ์ 0๋ถํฐ ์์
- ์ํ์ค ๊ฐ์ฒด(list, tuple, range, ๋ฌธ์์ด)์ ์ฌ์ฉ๊ฐ๋ฅ
- ์ํ์ค๊ฐ์ฒด[์ธ๋ฑ์ค]
์์ ์ธ๋ฑ์ค
- ์ธ๋ฑ์ค๋ฅผ ์์๋ก ์ง์ ํ๋ฉด ๋ค์์๋ถํฐ ์์์ ์ ๊ทผํ๊ฒ ๋๋ค.
- -1์ ๋ค์์ ์ฒซ ๋ฒ์งธ, -5๋ ๋ค์์ ๋ค์ฏ ๋ฒ์งธ ์์๋ฅผ ๋ปํ๋ค.
- ์ํ์ค ๊ฐ์ฒด(list, tuple, range, ๋ฌธ์์ด)์ ์ฌ์ฉ๊ฐ๋ฅ
example = ['Boostcamp', 'AI', 'Tech', 'penguin', 'sally', 'adc', 'heath']
print(example[5]) # adc
print(example[-2]) # adc
print(example[-4:]) # ['penguin', 'sally', 'adc', 'heath']
example = "BoostDevs"
print(example[:5]) # Boost
print(example[-4:-1]) # Dev๋ชจ๋
๋ชจ๋์ ํ์ด์ฌ ์ฝ๋๋ฅผ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋ฌถ์ด์ ๊ด๋ฆฌํ๊ณ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ ๊ฒ์ผ๋ก, ๋ณดํต ํ๋์ ํ์ด์ฌ .py ํ์ผ์ด ํ๋์ ๋ชจ๋์ด ๋๋ค. ๋ชจ๋ ์์๋ ํจ์, ํด๋์ค, ํน์ ๋ณ์๋ค์ด ์ ์๋ ์ ์์ผ๋ฉฐ, ์คํ ์ฝ๋๋ฅผ ํฌํจํ ์๋ ์๋ค.
๋ชจ๋์ ๊ดํ ์์ธํ ๋ด์ฉ์ #9. ๋ชจ๋ ์ฐธ๊ณ !
ํจํค์ง
ํจํค์ง๋ ํน์ ๊ธฐ๋ฅ๊ณผ ๊ด๋ จ๋ ์ฌ๋ฌ ๋ชจ๋์ ๋ฌถ์ ๊ฒ์ผ๋ก ํจํค์ง๋ ๋ชจ๋์ namespace๋ฅผ ์ ๊ณตํ๋ค. ํจํค์ง๋ ํ๋์ ๋๋ ํ ๋ฆฌ์ ๋์ฌ์ง ๋ชจ๋๋ค์ ์งํฉ์ ๊ฐ๋ฆฌํค๋๋ฐ, ๊ทธ ๋๋ ํ ๋ฆฌ์๋ ์ผ๋ฐ์ ์ผ๋ก __init__.py ๋ผ๋ ํจํค์ง ์ด๊ธฐํ ํ์ผ์ด ์กด์ฌํ๋ค.
ํจํค์ง๋ ๋ชจ๋๋ค์ ์ปจํ ์ด๋๋ก์ ํจํค์ง ์์๋ ๋๋ค๋ฅธ ์๋ธ ํจํค์ง๋ฅผ ํฌํจํ ์๋ ์๋ค. ํ์ผ์์คํ ์ผ๋ก ๋น์ ํ๋ฉด ํจํค์ง๋ ์ผ๋ฐ์ ์ผ๋ก ๋๋ ํ ๋ฆฌ์ ํด๋นํ๊ณ , ๋ชจ๋์ ๋๋ ํ ๋ฆฌ ์์ ํ์ผ์ ํด๋นํ๋ค.
ํจํค์ง๋ค์ ๋ชจ์์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ํ ๋ด์ฉ์ #52. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฐธ๊ณ !
- ํ์ด์ฌ ํจํค์ง์ ๋ชจ๋ ์์๋ณด๊ธฐ - ์นํจ
- ํจํค์ง - ์์ ๋ก ๋ฐฐ์ฐ๋ ํ์ด์ฌ ํ๋ก๊ทธ๋๋ฐ
os ๋ชจ๋์ import ํ ํ os.remove() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ์ ์ญ์ ํ ์์๋ค.
import os
os.remove("ai-tech-interview.txt")์์ธํ ๋ด์ฉ์ os.remove - Python documentation ์ฐธ๊ณ !
Python์ Built-in type์ ์๋์ ๊ฐ๋ค.
- Integer
- Floating-point
- Complex number
- String
- Boolean
- Built-in function
์์ธํ ๋ด์ฉ์ Built-in Types - Python documentation ์ฐธ๊ณ !
๐ก ๋นํธ์ธbuilt-in์ด๋?
์ด๋ค ๊ธฐ๋ฅ์ด๋ ํจ์ ๋ฑ์ด ๋ด์ฅ ๋๋ ๋นํธ์ธ๋์ด ์๋ค๋ ๋ป์ ๊ทธ๊ฒ์ด ํ๋ก๊ทธ๋จ์์ ๋ฐ๋ก ์ฌ์ฉ๊ฐ๋ฅํ๋๋ก ์ค๋น๋์ด ์๋ค๋ ๋ป์ด๋ค. ์์ธํ ๋ด์ฉ์ basic-terminology-in-programming - shoark7 ์ฐธ๊ณ ํ ๊ฒ.
numpy array๋ ํ๋์ ๋ฐ์ดํฐ ํ์ ๋ง ์ ์๊ฐ ๊ฐ๋ฅํ๋ค. Python list์ ๋ฌ๋ฆฌ ๋ค์ด๋๋ฏน ํ์ดํ์ ์ง์ํ์ง ์์ผ๋ฉฐ, C์ Array๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์๋๊ฐ ๋น ๋ฅด๋ค.
Python list๋ ๋ฐ์ดํฐ ์ฃผ์๊ฐ์ ์ ์ฅํ๊ณ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋๋ ํด๋น ์ฃผ์์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค. ๋ฐ๋ฉด Numpy array๋ C์ ๋ฐฐ์ด๊ณผ ์ ์ฌํ์ฌ ์ฐ์๋ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์์ด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๋๋ ์์๋๋ก ๊ฐ์ ธ์ค๋ฉด ๋๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๋ค.
Numpy์ ๋ํ ๋ด์ฉ์ #66. Numpy ์ฐธ๊ณ !
append(), extend(), insert() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ list์ value๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
append()
list.append(x) ํํ๋ก ์ฌ์ฉํ๋ฉฐ,
nums = [1, 2, 3]
nums.append(4)
print(nums) # [1, 2, 3, 4]
nums.append([5, 6])
print(nums) # [1, 2, 3, 4, [5, 6]]extend()
list.extend(iterable) ํํ๋ก ์ฌ์ฉํ๋ฉฐ,
nums = [1, 2, 3]
nums.extend([4])
print(nums) # [1, 2, 3, 4]
nums.extend([5, 6])
print(nums) # [1, 2, 3, 4, 5, 6]insert()
- ์๊ฐ๋ณต์ก๋: ``
list.insert(i, x) ํํ๋ก ์ฌ์ฉํ๋ฉฐ,
nums = [1, 2, 3]
nums.insert(0, 10)
print(nums) # [10, 1, 2, 3]
nums.insert(-1, 99)
print(nums) # [10, 1, 2, 99, 3]
nums.insert(len(nums), [20, 30])
print(nums) # [10, 1, 2, 99, 3, [20, 30]]
+์ฐ์ฐ์
list_1 + list_2 ํํ๋ก ์ฌ์ฉํ๋ฉฐ,
nums = [1, 2, 4, 6, 1, 5]
print(nums + [10, 9, 8, 7]) # [1, 2, 4, 6, 1, 5, 10, 9, 8, 7]- ํ์ด์ฌ append( ), extend( ), insert( ) ํจ์ ์ฐจ์ด / ์์์ถ๊ฐํจ์ ๋น๊ต (Python) - ์์ง๊ณต์ง
- ํ์ด์ฌ ์๋ฃํ ๋ณ ์ฃผ์ ์ฐ์ฐ์์ ์๊ฐ ๋ณต์ก๋ (Big-O) - ์ด๋ณด๋ชฝํค์ ๊ฐ๋ฐ๊ณต๋ถ๋ก๊ทธ
remove(), pop() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ list์ value๋ฅผ ์ญ์ ํ ์ ์๋ค.
remove()
remove()๋ ์ง์ฐ๊ณ ์ ํ๋ ์ธ๋ฑ์ค๊ฐ ์๋, ๊ฐ์ ์
๋ ฅํ๋ ๋ฐฉ์์ด๋ค. ๋ง์ฝ ์ง์ฐ๊ณ ์ ํ๋ ๊ฐ์ด ๋ฆฌ์คํธ ๋ด์ 2๊ฐ ์ด์์ด ์๋ค๋ฉด ์์์ ๊ฐ์ฅ ์์ ์๋ ๊ฐ์ ์ง์ฐ๊ฒ ๋๋ค. ๊ฐ์ ์ญ์ ํ ๋ ์ญ์ ๋ ๊ฐ์ ๋ฐํํ์ง ์๋๋ค. remove()๋ ์๊ฐ๋ณต์ก๋
example = [1, 2, 3, 4, 5, 1]
example.remove(1)
print(example) # [2, 3, 4, 5, 1]pop()
pop()์ ๋ฆฌ์คํธ์์ ์ง์ฐ๊ณ ์ ํ๋ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐ์์ ์ง์ฐ๋ ๋ฐฉ์์ด๋ค. ๊ฐ์ ์ญ์ ํ ๋ ์ญ์ ๋ ๊ฐ์ ๋ฐํํ๋ค. ์ธ๋ฑ์ค๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ๋ฆฌ์คํธ์ ๋ง์ง๋ง ์์๊ฐ ์ญ์ ๋๋ฉฐ ๋ฐํ๋๋ค. pop()์ ์๊ฐ๋ณต์ก๋
example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(example.pop()) # 10
print(example) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(example.pop(3)) # 4
print(example) # [1, 2, 3, 5, 6, 7, 8, 9, 10]del
del list[i] ํํ๋ก ์ฌ์ฉํ๋ฉฐ, ์๊ฐ๋ณต์ก๋
example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del example[7]
print(example) # [1, 2, 3, 4, 5, 6, 7, 9, 10]
example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del example[7:]
print(example) # [1, 2, 3, 4, 5, 6, 7]Python์ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ค. Python์ ์ฃผ์ OOP ๊ฐ๋ ์๋ Class, Object, Method, Inheritance(์์), Polymorphism(๋คํ์ฑ), Data Abstraction(๋ฐ์ดํฐ ์ถ์ํ), Encapsulation(์บก์ํ)์ ํฌํจํ๋ค.
๋ ์์ธํ ๋ด์ฉ์ #55. Inheritance, #59. Polymorphism, #60. Encapsulation, #61. Data Abstraction ์ฐธ๊ณ !
Shallow copy๋ ์๋ก์ด ๊ฐ์ฒด(๋ณ์)๋ฅผ ๋ง๋ ํ์ ์๋ณธ์ ์ ๊ทผํ ์ ์๋ ์ฐธ์กฐ(reference)๋ฅผ ์ ๋ ฅํ๋ค. ์ด๋ฐ ๊ฒฝ์ฐ ์๋ก ๋ค๋ฅธ ๋ณ์๋ช ์ด์ง๋ง ๋ณธ์ง์ ์ผ๋ก ์๋ก ๊ฐ์ ๋์์ ์๋ฏธํ๋ฏ๋ก ํ๋์ ๋ณ์ ์ญ์ ์์ ์ด ๋๋ค.
๊ฐ๋ณํ(mutable) ์๋ฃํ์ ๋ํด์ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ค. ๊ฐ๋ณํ(mutable) ์๋ฃํ์ ๊ฐ์ ์ฃผ์์์ ๊ฐ(value)์ด ๋ณ๊ฒฝ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ์์ ๋ณต์ฌ๊ฐ ๊ฐ๋ฅํ๋ค. ๋ฐ๋ฉด ๋ถ๋ณํ(immutable) ์๋ฃํ์ ๋ณธ์ง์ ์ผ๋ก ๋ณ๊ฒฝ์ด ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ์ฌ๋ฐฐ์ ์ ํตํด ๋ณ์๋ฅผ ๋ฐ๊พผ๋ค. ๋ฐ๋ผ์ ์ฌ๋ฐฐ์ ์ด ์ด๋ฃจ์ด์ง๋ฏ๋ก ๊ฐ์ฒด๊ฐ ์๋ก ๋ฌ๋ผ์ง๋ค.
a = [1, 2, 3, 4, 5]
b = a # shallow copy
a[1] = 10
print(a, b) # [1, 10, 3, 4, 5] [1, 10, 3, 4, 5]Deep copy๋ ์๋ก์ด ๊ฐ์ฒด(๋ณ์)๋ฅผ ๋ง๋ ๋ค์ ์๋ณธ์ ๋ณต์ฌ๋ณธ์ ๋ณ์์ ์ ๋ ฅํ๋ค. ์๋ก ๊ฐ๋ง ๊ฐ์ ๋ฟ ๋ณธ์ง์ ์ผ๋ก ์๋ก ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ํ ๋ณ์๊ฐ ์์ ๋ ์ ๋ค๋ฅธ ๋ณ์๊ฐ ์์ ๋์ง ์๋๋ค.
a = [1, 2, 3, 4, 5]
b = a[:] # deep copy
a[1] = 10
print(a, b) # [1, 10, 3, 4, 5] [1, 2, 3, 4, 5]import copy
a = [1, 2, 3, 4, 5]
b = copy.deepcopy(a) # deep copy
a[1] = 10
print(a, b) # [1, 10, 3, 4, 5] [1, 2, 3, 4, 5]ํ์ด์ฌ์์ ๋ฉํฐ ์ฐ๋ ๋๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ threding ๋ชจ๋(High level)์ ์ฌ์ฉํ๊ฑฐ๋ thread ๋ชจ๋(Low level)์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. ํ์ฌ thread ๋ชจ๋์ deprecated ๋์ด threading ๋ชจ๋์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค.
๋ฉํฐ์ค๋ ๋ฉ์ ์ฌ์ฉํ๋ฉด ๋น์ฐํ ์๋๊ฐ ๋นจ๋ผ์ง ๊ฒ์ด๋ผ ์๊ฐํ ์ ์์ง๋ง, ํ์ด์ฌ์ GIL(Global Interpreter Lock) ์ ์ฑ ์ผ๋ก ์ธํด, ๋ฉํฐ์ค๋ ๋ฉ์ ์ฌ์ฉํ๋ค ํ๋๋ผ๋, ์๋๋ ์ฑ๊ธ์ค๋ ๋์ ๋ณ๋ฐ ๋ค๋ฅด์ง ์๋ค.
ํ๋์ ์์์ ์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ์๋ฌด๋ฐ ๊ท์น์์ด ์ ๊ทผํ๋ฉด, ์์ ๋๊ธฐํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์ ์์์ lock์ ๋๋๋ฐ, Python์ ๋ชจ๋ ์์์ lock์ globalํ๊ฒ ๊ด๋ฆฌํ๊ณ ์๋ค. ํ๋ฒ์ ํ๋์ ์ค๋ ๋๋ง ์์์ ์ ๊ทผํ ์ ์๋ค๋ ๊ฒ์ด๋ค. ์ด๋ก์ธํด, ์์์ ๊ณต์ ํ๋ ์ฌ๋ฌ ์ค๋ ๋๋ฅผ ๋์์ ์คํ์ํจ๋ค๊ณ ํด๋, ๊ฒฐ๊ตญ GIL ๋๋ฌธ์ ํ๋ฒ์ ํ๋์ ์ค๋ ๋๋ง ์คํ๋๋ ๊ฒ์ด๋ค.
๐ก ๋ฉํฐ์ค๋ ๋ฉ์ด ์ ์ฉํ ๊ฒฝ์ฐ
GIL์ cpu ๋์์ ๋ํด์๋ง ์ ์ฉ๋๋ค. ์ฐ๋ ๋๊ฐ cpu ๋์์ ๋ง์น๊ณ I/O ์์ ์ ์คํํ๋ ๋์์๋ ๋ค๋ฅธ ์ฐ๋ ๋๊ฐ cpu ๋์์ ๋์์ ์คํํ ์ ์๋ค. ๋ฐ๋ผ์ cpu ๋์์ด ๋ง์ง ์๊ณ I/O๋์์ด ๋ ๋ง์ ํ๋ก๊ทธ๋จ์์๋ ๋ฉํฐ ์ฐ๋ ๋๋ง์ผ๋ก ์ฑ๋ฅ์ ์ผ๋ก ํฐ ํจ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
- [Python] ํ์ด์ฌ ๋ฉํฐ ์ฐ๋ ๋(thread)์ ๋ฉํฐ ํ๋ก์ธ์ค(process) by Nathan Kwon
- [์ด์์ฒด์ ] Concurrency : locks- blackinkgj
- What is GIL in Python language?
ํ์ด์ฌ ํ์ผ(.py)๋ฅผ ์คํํ๋ฉด, ์์ค ์ฝ๋๋ ๋ฐ์ดํธ ์ฝ๋(byte code)๋ก ๋ณํ๋๋ฉฐ, .pyc, .pyo ํ์ผ ํ์์ผ๋ก ์ ์ฅ๋๋ค. ์ด ๋ ์์ค ์ฝ๋๋ฅผ ๋ฐ์ดํธ ์ฝ๋๋ก ๋ณํํ๋ ๊ณผ์ ์ ์ปดํ์ผ(compilation) ๋จ๊ณ๋ผ๊ณ ํ๋ค.
ํ์ด์ฌ ๊ฐ์๋จธ์ (Python Virtual Machine)์ด ๋ฐ์ดํธ ์ฝ๋๋ฅผ ๊ธฐ๊ณ์ด(machine code)๋ก ๋ณํํ์ฌ ์ด๋ค ์ด์์ฒด์ ๋ ์คํํ ์ ์๋๋ก ํ๋ค. ์ด ๋ ์ฐ๋ฆฌ์ ์ฝ๋์ ์ธํฐํ๋ฆฌํฐ๊ฐ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐ๊ฒฐ์ํค๋ ๊ณผ์ ์ด ์๋๋ฐ, ์ด๋ฅผ ๋งํฌ(linking) ๋จ๊ณ๋ผ๊ณ ํ๋ค.
์ฐธ๊ณ ๋ก dis ๋ชจ๋์ ์ฌ์ฉํ์ฌ ์์ค ์ฝ๋๊ฐ ์ด๋ค ๋ฐ์ดํธ ์ฝ๋๋ก ๋ณํ๋๋์ง ํ์ธํ ์ ์๋ค.
import dis
def mult(a, b):
return a*b
dis.dis(mult)
# output:
# 4 0 LOAD_FAST 0 (a)
# 2 LOAD_FAST 1 (b)
# 4 BINARY_MULTIPLY
# 6 RETURN_VALUE- Compiling and Linking in Python - Net-informations.com
- What is the process of compilation and linking in python? - tutorialspoint
- Python Compilation/Interpretation Process - stackoverflow
- How does Python work? - towards data science
- Is Python interpreted or compiled? Yes. - Ned Batchelder
- Can Python be compiled? is it compiled or interpreted? - astateofdata
ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ํจํค์ง์ ๋ชจ์์ด๋ค.
์ฃผ๋ก ์ฌ์ฉ๋๋ ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก๋ Numpy, Pandas, Matplotlib, Scikit-learn ๋ฑ์ด ์๋ค.
ํจํค์ง์ ๋ํ ๋ ์์ธํ ๋ด์ฉ์ #42. ํจํค์ง ์ฐธ๊ณ !
split()์ ํน์ ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋ถ๋ฆฌํ ๋ ์ฌ์ฉํ๋ค.
str.split(sep=None, maxsplit=-1)sep์ ๊ตฌ๋ถ์๋ก ์ฌ์ฉํ์ฌ ๋ฌธ์์ด์ ์๋ ๋จ์ด list๋ฅผ ๋ฐํํ๋ค.
sep์ด ์ง์ ๋๋ฉด ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋ถ๋ฆฌํ๊ณ , sep์ด ์ง์ ๋์ง ์์๊ฑฐ๋ None์ธ ๊ฒฝ์ฐ์๋ whitespace๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋ถ๋ฆฌํ๋ค.
maxsplit์ด ์ง์ ๋๋ฉด ๊ทธ ์๋งํผ์ ๋ถํ ์ด ์ํ๋๊ณ , maxsplit์ด ์ง์ ๋์ง ์์๊ฑฐ๋ -1์ธ ๊ฒฝ์ฐ์๋ ๊ฐ๋ฅํ ๋ชจ๋ ๋ถํ ์ด ์ํ๋๋ค.
a = "ai tech interview"
print(a.split()) # ['ai', 'tech', 'interview']
a = "ai tech interview"
print(a.split()) # ['ai', 'tech', 'interview']
a = "ai-tech-interview"
print(a.split("-")) # ['ai', 'tech', 'interview']
a = "ai-tech-interview"
print(a.split("-", 1)) # ['ai', 'tech-interview']import [ํจํค์ง/๋ชจ๋]
from [ํจํค์ง] import [๋ชจ๋/๋ณ์/ํจ์/ํด๋์ค]
from [๋ชจ๋] import [๋ณ์/ํจ์/ํด๋์ค]import ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋์ ๊ฐ์ ธ์ฌ ์ ์๋ค. ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋ชจ๋์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
import numpy # importing using the original module name
import numpy as np # importing using an alias name
from numpy import * # imports everything present in the numpy modulefrom numpy import argmax as arm # ์ด๋ฐ๊ฑฐ๋ ๋๋ค
nums = [1, 2, 3, 4, 5]
print(arm(nums)) # 4์์์ ํตํด ์์ (๋ถ๋ชจ) ํด๋์ค์ ๋ฉค๋ฒ ํจ์, ๋ฉค๋ฒ ๋ณ์๋ค์ ๋ชจ๋ ํ์ (์์) ํด๋์ค๊ฐ ๊ฐ์ง ์ ์๋ค. ์์ ํด๋์ค๋ฅผ ์์ํจ์ผ๋ก์จ ์ฝ๋ ์ฌ์ฌ์ฉ์ฑ์ด ๋ ์ข์์ง๊ณ , ๊ด๋ฆฌ๊ฐ ์ฉ์ดํด์ง๋ค.
ํ์ด์ฌ์ ๋ถ๋ชจ ํด๋์ค A ๋ฅผ ์์ ํด๋์ค B ๊ฐ ์์ํ๋ Single Inheritance, ๋ถ๋ชจ ํด๋์ค A ๋ฅผ ์์ ํด๋์ค B ๊ฐ ๋ค์ B ๋ฅผ ์์ ํด๋์ค C ๊ฐ ์์ํ๋ Multi-level Inheritance, ๋ถ๋ชจ ํด๋์ค A ๊ฐ ์ฌ๋ฌ ์์ ํด๋์ค์ ์์๋๋ Hierarchical Inheritance, ํ๋์ ์์ ํด๋์ค๊ฐ ์ฌ๋ฌ ๋ถ๋ชจ ํด๋์ค๋ฅผ ์์ํ๋ Multiple Inheritance ๊ฐ ์๋ค.
class ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์๋ค. ์ด ๋, ํด๋์ค๋ช ์ ๊ดํธ์ ์์๋ฐ์ ๋ถ๋ชจ ํด๋์ค๋ฅผ ์ค์ ํ ์๋ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก __init__ ์ด๋ผ๋ ๋งค์ง ๋ฉ์๋๋ฅผ ํตํด ๋ฉค๋ฒ ๋ณ์๋ค์ ์ธํ
ํ ์ ์๋ค. ์์ธํ๋ ํด๋์ค๊ฐ ๊ฐ์ฒด๋ก ์ ์ธ๋ ๋, ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ์ด๊ธฐํํ๋ ์ญํ ์ ๋ด๋นํ๋ค.
ํด๋์ค ๋ด์์๋ ๋ฉค๋ฒ ํจ์๋ฅผ ๋ง๋ค ์ ์๊ณ , ํด๋์ค ๊ฐ์ฒด์์ ๋ฉค๋ฒ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
class MyClass():
def __init__(self, feature):
self.feature = feature
...
def my_method(self):
...์ฃผ๋ก ํ ์คํธ๋ฅผ ์ํด ๋ง์ด ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ผ๋ก, ์ด๋ค ํด๋์ค๋ ๋ชจ๋์ ์ผ๋ถ (ํจ์๋ ๋ณ์ ๋ฑ) ๋ฅผ ๋ก์ปฌ์์ ๋ฐํ์์ผ๋ก๋ง instance ๋ฅผ ํตํด ์์ ํ๋ ๋ฐฉ๋ฒ์ ๋งํ๋ค.
์์๋ก heath.py ํ์ผ์ A ํด๋์ค์ a ๋ผ๋ ํจ์๊ฐ ์๋๋ฐ, ๋ค๋ฅธ ํ์ผ์์ A ๋ฅผ import ํ์ฌ a ํจ์ ๋์ new_a ๋ฅผ ํ ๋นํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
from heath import A
A.a = new_a
my_A = A() # A ํด๋์ค ๊ฐ์ฒด ํ ๋น
my_A.a # new_a ๊ฐ ๋์ํ์ด์ฌ์ ์๋ฐ์ ๋ค๋ฅด๊ฒ multiple inheritance ์ ์ง์ํ๋ค. multiple inheritance ์ ๊ฐ๋ ์ #55 ์์ ์ฐธ๊ณ ํ ์ ์๋ค.
์์๋ ์๋์ ๊ฐ๋ค.
class P_A():
...
class P_B():
...
class C(P_A, P_B): #P_A ์ P_B ํด๋์ค๋ฅผ ๋์์ ์์
...๋คํ์ฑ์ ๊ฐ์ฒด์งํฅ์ ์ฃผ์ ๊ฐ๋ ์ผ๋ก ์ฌ๋ฌ๊ฐ์ง ํํ๋ฅผ ๊ฐ์ง ์ ์๋ ๋ฅ๋ ฅ์ ๋งํ๋ค. ๋คํ์ฑ์ ์ฝ๋์ ์ ์ง๋ณด์์ ๋์์ ์ค๋ค.
ํ์ด์ฌ์ ๋คํ์ฑ์ ์ง์ํ๋๋ฐ, + ์ฐ์ฐ์ด๋ len ์ฐ์ฐ์ ๋ํด ์๊ฐํด๋ณผ ์ ์๋ค. ์ด๋ค์ ์ฌ๋ฌ ํ์ ์ ๋ณ์์ ๋ํด์๋ ๋์ผํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋๋ฐ overriding ๊ณผ overloading ์ ํตํด ๊ฐ๊ธฐ ๋ค๋ฅธ ํ์ ์ ๋ณ์์๋ ๋ฐ์ํ๋๋ก ๋คํ์ฑ์ ์ฃผ์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค.
์บก์ํ๋ ์ฃผ์ ๋ณ์๋ ํจ์๋ฅผ ์ธ๋ถ๋ก๋ถํฐ ๋ณดํธํ๋ ๋ฐฉ๋ฒ์ ๋งํ๋ค. ์บก์ํ๋ฅผ ํตํด ์ฝ๋์ ์์ ์ฑ์ ๋์ผ ์ ์๋ค.
ํ์ด์ฌ์์๋ ํด๋์ค๋ฅผ ์๊ฐํด๋ณผ ์ ์๋ค. ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์๋ ๋ฉค๋ฒ ํจ์์ ์ ๊ทผํ๊ธฐ ์ํด์๋ ํด๋์ค์ ๋ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค. ๊ฐ์ฒด๋ฅผ ํตํด ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ๋๋ฌธ์ ์ง์ ๋ณ์๋ฅผ ์๋๋ ๊ฒ๋ณด๋ค ๋ฐ์ดํฐ๋ฅผ ๋ ์์ ํ๊ฒ ์งํฌ ์ ์๋ค.
๋ฐ์ดํฐ ์ถ์ํ๋ ๊ฐ์ฒด์งํฅ์ ์ฃผ์ ๊ฐ๋ ์ผ๋ก ์ฌ์ฉ์์๊ฒ ๋ฐ์ดํฐ์ ์ฃผ์ ์ ๋ณด๋ง ์ ๊ณตํ์ฌ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ๋ชฐ๋ผ๋ ์ฌ์ฉํ ์ ์๊ฒ ๋ง๋๋ ๋ฐฉ๋ฒ์ด๋ค.
ํ์ด์ฌ์์๋ abstract class ๋ฅผ ํตํด ๋ฐ์ดํฐ ์ถ์ํ๋ฅผ ํ ์ ์๋ค. abstract class ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ abc ๋ชจ๋์ import ํ๊ณ metaclass=ABCClass ์ @abstractmethod ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
from abc import *
class ์ถ์ํด๋์ค๋ช
(metaclass=ABCMeta):
@abstractmethod
def ์ถ์๋ฉ์๋(self):
passํ์ด์ฌ์ ๋ค๋ฅธ ์ธ์ด์ ๋ฌ๋ฆฌ private, protected ๋ฑ์ ์ ๊ทผ ์ ํ์๋ฅผ ์ง์ ๋ช ์ํ์ง ์๊ณ ๋ณ์๋ช ์ ํตํด ์ ๊ทผ ์ ์ด๋ฅผ ํ๋ค.
์ ๋์ฌ _ ๊ฐ ํ ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ protected, ์ ๋์ฌ _ ๊ฐ ๋ ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ private, ์ ๋์ฌ _ ๊ฐ ์๊ฑฐ๋ ์ ๋ฏธ์ฌ _ ๊ฐ ๋ ๊ฐ ์ด์ ์๋ ๊ฒฝ์ฐ์๋ public ์ด๋ค.
ํ์ด์ฌ์์ ํด๋์ค ๋ด๋ถ์ ์๋ฌด ๋ด์ฉ ์์ด ์ ์ธ๋ง ํ๊ธฐ ์ํด์๋ pass ๋ ... ์ ์ฌ์ฉํ ์ ์๋ค. ์ถ๊ฐ์ ์ผ๋ก empty class ๋ฅผ ์ ์ธํ ํ, ์ธ๋ถ์์ ๊ฐ์ฒด๋ฅผ ํตํด ํด๋์ค์ ๋ณ์๋ ํจ์๋ฅผ ๋ง๋ค ์๋ ์๋ค.
class empty():
... # or pass
e = empty()
e.a = 10
print(e.a) # 10ํ์ด์ฌ์ ๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด์ด๋ค. ๋ฐ๋ผ์ ๊ธฐ๋ณธ์ ์ผ๋ก object ํด๋์ค๋ฅผ ์์๋ฐ๊ณ ์๋ค. object() ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ก์ด ๊ธฐ๋ณธ object ๊ฐ์ฒด๋ฅผ ๋ฐํ๋ฐ์ ์ ์๋ค.
map ํจ์๋ iterable ํ ๊ฐ์ฒด์ ๋ชจ๋ ์์์ ๋์ผํ ํจ์๋ฅผ ์ ์ฉํ๋ ๊ธฐ๋ฅ์ ํ๋ค.
์ฒซ ์ธ์๋ก ์ ์ฉํ ํจ์๋ฅผ, ๋๋ฒ์งธ ์ธ์๋ก iterable ํ ๊ฐ์ฒด๋ฅผ ๋ฃ์ผ๋ฉด, iterable ํ map ๊ฐ์ฒด ํํ๋ก ๊ฐ ์์์ ๋ํด ํจ์๊ฐ ์ ์ฉ๋ ๋ฌถ์๋ค์ด ๋ด๊ฒจ ๋์จ๋ค.
int_arr = list(map(int, input().split()))ํ์ด์ฌ์ ๋ฆฌ์คํธ๋ ๊ฐ ์์๋ค์ ๊ฐ์ ์ง์ ์ฌ์ฉํ์ง ์๊ณ ์์๋ค์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๋ ๋ฐฉ์์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์์๋ค์ ํ์ ์ด ์ ํด์ง์ง ์์ ํธ๋ฆฌํ์ง๋ง ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ณ ๋๋ฆฌ๋ค๋ ๋จ์ ์ด ์๋ค.
๋ฐ๋ฉด, ๋ํ์ด๋ C ๊ธฐ๋ฐ์ผ๋ก ๊ตฌํ๋์ด ์์๋ค์ ํ์ ์ ๋ฏธ๋ฆฌ ์ค์ ํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ๊ฒ ์ฌ์ฉํ๊ณ ๋น ๋ฅด๋ค. ๋ํ ํ๋ ฌ๊ณผ ์ ํ๋์์ ํธ๋ฆฌํ ํจ์๋ค์ ์ ๊ณตํ๋ค๋ ์ฅ์ ๋ ์๋ค.
๋ฉํฐ์ฐ๋ ๋ฉ์ ํ ๋, ๊ณต์ ์์์ ๋ํด ์ฌ๋ฌ ์ฐ๋ ๋๊ฐ ๋์์ ์ ๊ทผํ๋ค๋ฉด ๊ฐฑ์ ๋ ๋ด์ฉ์ด ์ ์ค๋๋ ๋ฑ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค. ์ด๋ฅผ ๋ง๊ธฐ ์ํด ํ์ด์ฌ์ GIL (Global Interpreter Lock) ์ ํตํด python interpreter ์ ํ ์ฐ๋ ๋๋ง ์ ๊ทผํ์ฌ ๋ชจ๋ ์์์ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ค.
์ ํํ๋ ๋ฉํฐ ์ฐ๋ ๋๊ฐ bytecode(=instruction) ํ ๋ผ์ธ์ฉ์ ๋ค๊ณ ์๊ธฐ ๋๋ฌธ์, ํ ์ฐ๋ ๋์ bytecode ํ ์ค์ ๋ํด์๋ง GIL ์ ํ์ฉํ๋ค.
- [python] GIL, Global interpreter Lock์ ๋ฌด์์ผ๊น? - ์ํ๊ณผ์ ์ข์ถฉ์ฐ๋ ํ๋ก๊ทธ๋๋ฐ
- ์ Python์๋ GIL์ด ์๋๊ฐ - ๊ฐ๋ฐ์๋ฐ๋ธ๋ก๊ทธ
ํ์ด์ฌ์ ์ผ๋ฐ์ ์ผ๋ก C ๋ก ๊ตฌํ๋ ์ธํฐํ๋ฆฌํฐ ์ธ์ด์ด๋ค. ์ผ๋ฐ์ ์ธ C ์ธ์ด์ ๊ตฌ๋ถํ๊ธฐ ์ํด ํ์ด์ฌ ๊ตฌํ์ฒด C ๋ฅผ CPython ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
CPython ์ ์ธํฐํ๋ฆฌํฐ์ด๋ฉด์ ์ปดํ์ผ๋ฌ๋ก Python ์ฝ๋๋ฅผ C ๊ฐ ์๋ bytecode ๋ก ์ปดํ์ผํด์ฃผ๊ณ , ์ด๋ฅผ interpreter(virtual machine) ๊ฐ ์คํํ๊ฒ ๋ง๋ ๋ค. ์ด๋ฌํ CPython ์ interpreter ์ ์ธ ํน์ง์ด ํ์ด์ฌ์ ์ฐจ๋ณ๋๊ฒ ๋ง๋ค์๋ค.
ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ณ ๋ด๋ถ ํจ์์์ ์ธ์๋ก ๋ฐ์ ํจ์๋ฅผ ์ฌ์ฉํ๋ ํด๋์ค๋ ํจ์๊ฐ ์์ ๋, ์ธ์๋ก ์ฌ์ฉํ ํจ์๋ฅผ ๊ฐํธํ๊ฒ ์ง์ ํด์ฃผ๋ ์ญํ ์ ํ๋ ๊ฒ์ด Decorator ์ด๋ค.
Decorator ์ ์ฌ์ฉ ๋ฌธ๋ฒ์ ์ธ์๊ฐ ๋ ํจ์ ์์ @์ธ๋ถํจ์์ด๋ฆ ์ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค.
์๋ ์์๋ฅผ ๋ณด๋ฉด, Decorator ๋ฅผ ํตํด big_number ์ big_number2 ๋ผ๋ ์๋ก ๋ค๋ฅธ ํจ์๋ฅผ make_time_checker ๊ฐ ์ธ์๋ก ๋ฐ์ ๋ด๋ถ ํจ์์์ ์ฌ์ฉํ๊ณ ์๋ค.
import time
def make_time_checker(func):
def new_func(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
print('์คํ์๊ฐ:', end_time - start_time)
return result
return new_func
@make_time_checker
def big_number(n):
return n ** n ** n
@make_time_checker
def big_number2(n):
return (n+1) ** (n+1) ** (n+1)ํ์ด์ฌ์์๋ ๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด์ด๋ฏ๋ก ๋ณ์๋ค์ ๊ฐ์ ๋ฐ๋ก ๊ฐ์ง์ง ์๊ณ ๊ฐ์ ๊ฐ์ง ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ฒ ๋๋ค.
object interning ์ ์์ฃผ ์ฌ์ฉ๋ , ์ฆ ์ฌํ์ฉ๋ object ์ ๋ํด ๋งค๋ฒ ์๋ก์ด ์ฃผ์๋ฅผ ํ ๋นํ๋ ๊ฒ์ ๋นํจ์จ์ ์ด๋ฏ๋ก, ํ๋์ ์ฃผ์์ ๊ฐ์ ์ฃผ๊ณ ๊ทธ ์ฃผ์๋ฅผ ์ฌํ์ฉํ๋ ์์ ์ ๋งํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ํ์ด์ฌ์์๋ -5~256, [a-Az-Z0-9_] ์ ๋ํด ๊ณ ์ ๋ ์ฃผ์๋ฅผ ํ ๋นํ์ฌ interning ์ ํ๊ณ ์๋ค.
@classmethod
ํด๋์ค ๋ด๋ถ์ ํจ์ ์ค์ @classmethod ๋ก ์ ์ธ๋ ํจ์์ ๋ํด์๋ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ๋ฐ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. ํ์ง๋ง ํจ์์ ์ฒซ ์ธ์๋ก ํด๋์ค๋ฅผ ๋ฐ์์, ์์๋์์ ๋ ์์ ํด๋์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ฅด๋ ํน์ง์ด ์๋ค.
@staticmethod
@staticmethod ๋ @classmethod ์ ๋ง์ฐฌ๊ฐ์ง๋ก ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ง ์๊ณ ๋ ๋ฐ๋ก ์ ๊ทผํ ์ ์๋ค. ํ์ง๋ง ํด๋์ค๋ฅผ ์ธ์๋ก ๋ฐ์ง ์๊ธฐ ๋๋ฌธ์, ์์๋์์ ๋์๋ ์์ ํด๋์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ฅด์ง ์๊ณ ์ฒ์์ ํด๋์ค์์ ์ ์ธํ ๋ฐ์ดํฐ๋๋ก ํจ์๊ฐ ์ฌ์ฉ๋๋ค.
class Language:
default_language = "English"
def __init__(self):
self.show = '๋์ ์ธ์ด๋' + self.default_language
@classmethod
def class_my_language(cls):
return cls()
@staticmethod
def static_my_language():
return Language()
def print_language(self):
print(self.show)
class KoreanLanguage(Language):
default_language = "ํ๊ตญ์ด"
>>> from language import *
>>> a = KoreanLanguage.static_my_language()
>>> b = KoreanLanguage.class_my_language()
>>> a.print_language()
๋์ ์ธ์ด๋English
>>> b.print_language()
๋์ ์ธ์ด๋ํ๊ตญ์ด@property
๊ฐ์ฒด์งํฅ ์ธ์ด์์๋ ์บก์ํ๋ฅผ ์ํด ๋ณ์๋ฅผ ์ง์ ์ง์ ํ์ง ์๊ณ ๊ฐ์ฒด์ ํจ์๋ฅผ ํตํด ์ง์ ํ๊ณ ๋ฐ์์ค๋ setter, getter ํจ์๋ฅผ ์ฌ์ฉํ๋ค. ํ์ด์ฌ์์๋ ์ด๋ฅผ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก @property ๋ฅผ ์ ๊ณตํ๋ค.
getter ๊ฐ ๋ ํจ์์ @property ๋ฅผ, setter ๊ฐ ๋ ํจ์์ @๋ณ์.setter ๋ฅผ ๋ถ์ด๋ฉด ๋๋ค.
class Test:
def __init__(self):
self.__color = "red"
@property
def color(self):
return self.__color
@color.setter
def color(self,clr):
self.__color = clr
if __name__ == '__main__':
t = Test()
t.color = "blue"
print(t.color)- 44. class ์ ๋ฆฌ, ์ ์ ๋ฉ์๋ @classmethod์ @staticmethod์ ์ ๋ฆฌ - ํ์ด์ฌ ๊ธฐ๋ณธ์ ๊ฐ๊ณ ๋ฆ์!
- ํ์ด์ฌ์์ @property ์ ๋ํด ์์๋ณด์. - HAMA ๋ธ๋ก๊ทธ





