-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77ddf0b
commit 0d108ba
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
title: dict.get hay or? | ||
date: 2023-07-12 | ||
modified: 2023-07-12 | ||
tags: python, dict, default, get, or | ||
category: news | ||
slug: dict.get | ||
authors: Pymier0 | ||
description: dict.get(k, default) có như dict.get(k) or default | ||
|
||
`dict.get(k, default)` có bằng `dict.get(k) or default`? | ||
|
||
`or` thường được dùng để trả về giá trị mặc định: | ||
|
||
``` | ||
host = os.environ.get("host") or "localhost" | ||
``` | ||
|
||
Nhưng: | ||
|
||
`or` có cách hoạt động khá lắt léo: | ||
|
||
các giá trị được biến thành boolean trước khi xử lý, với giá trị trong dict là | ||
0, "", [],... nó lại bị `or` coi là không có gì, và lấy vế phải ra dùng. | ||
|
||
Nên nếu dict là : | ||
|
||
```py | ||
d = {"red": 0, "yellow": 1, "green": 2} | ||
``` | ||
thì kết quả khác nhau: | ||
|
||
```py | ||
d.get("red", 3) == 0 | ||
``` | ||
còn | ||
|
||
```py | ||
d.get("red") or 3 == 3 | ||
``` | ||
|
||
### Kết luận | ||
|
||
```py | ||
get(key, default=None, /) method of builtins.dict instance | ||
Return the value for key if key is in the dictionary, else default. | ||
``` | ||
|
||
Đôi khi nhìn qua thì giống, nhưng phải sờ tận tay mới biết khác chỗ nào. | ||
|
||
Hết. | ||
|
||
Đăng ký ngay tại [PyMI.vn](https://pymi.vn) để học Python tại Hà Nội TP HCM (Sài Gòn), | ||
trở thành lập trình viên #python chuyên nghiệp ngay sau khóa học. |