Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
SungmanHan
python_da
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
python_da
/
step06_function
/
funcEx2.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
44 lines (34 loc) · 1.6 KB
master
Breadcrumbs
python_da
/
step06_function
/
funcEx2.py
Copy path
Top
File metadata and controls
Code
Blame
44 lines (34 loc) · 1.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 기본값 매개변수와 키워드 인자
print('---------- 기본값 ----------')
def greet(name, greeting='안녕하세요'): # greeting 은 안 넘기면 기본값
print(f'{name}님 {greeting}')
greet('홍길동')
greet('김길동', '반갑습니다')
# 기본값이 있는 매개변수는 반드시 뒤쪽에 와야 한다
# def wrong(greeting='안녕', name): # SyntaxError!
print('---------- 키워드 인자 ----------')
def profile(name, age, city):
print(f'{name} / {age}세 / {city}')
profile('홍길동', 23, '서울') # 위치로 넘기기
profile(name='홍길동', age=23, city='서울') # 이름으로 넘기기
profile(city='부산', name='김길동', age=30) # 이름을 쓰면 순서 무관
# 인자가 많아지면 이름을 쓰는 편이 읽기 좋다
# profile('홍길동', 23, '서울') vs profile(name=..., age=..., city=...)
print('---------- 기본값의 함정 ----------')
# 기본값으로 리스트를 주면 안 된다. 함수를 정의할 때 딱 한 번 만들어져
# 호출할 때마다 같은 리스트를 계속 쓴다.
def bad_append(item, box=[]):
box.append(item)
return box
print(bad_append('a')) # ['a']
print(bad_append('b')) # ['a', 'b'] ← 비어 있길 기대했는데!
print(bad_append('c')) # ['a', 'b', 'c']
# 올바른 방법 : None 을 기본값으로 두고 안에서 만든다
def good_append(item, box=None):
if box is None:
box = []
box.append(item)
return box
print(good_append('a')) # ['a']
print(good_append('b')) # ['b']
print(good_append('c')) # ['c']
You can’t perform that action at this time.