Trong Python, tôi hiện đang làm điều này:
if user_can_read[request.user, b] == False:
Có cách nào khác để kiểm tra xem chức năng có trả lại
if user_can_read[request.user, b]:
## do stuff
0 không?Hỏi ngày 23 tháng 8 năm 2012 lúc 18:30Aug 23, 2012 at 18:30
q3dq3dq3d
3.3837 Huy hiệu vàng33 Huy hiệu bạc38 Huy hiệu Đồng7 gold badges33 silver badges38 bronze badges
3
1 câu trả lời
Bạn chỉ có thể sử dụng
if user_can_read[request.user, b]:
## do stuff
Nếu
if user_can_read[request.user, b]:
## do stuff
1 trả về bất cứ điều gì [ngoại trừ 0, sai, v.v.], nó sẽ được coi là đúng và làm công cụ.Và phủ định:
if user_can_read[request.user, b]:
## do stuff
2Đã trả lời ngày 23 tháng 8 năm 2012 lúc 18:33Aug 23, 2012 at 18:33
JohnjohnJohn
Phù bằng vàng 14.8k1144 Huy hiệu bạc64 Huy hiệu đồng11 gold badges44 silver badges64 bronze badges
3
Sử dụng câu lệnh IF, bạn có thể tìm thấy hàm được trả về đúng hoặc không trong Python. Bước này là cần thiết một thời gian để tiến hành bước tiếp theo của một hàm riêng. Mã ví dụ đơn giản. Đầu ra:: if function_Name[argu]:
# do_something
def validate_age[age]:
if age > 20:
return True
else:
return False
# Print value
print[validate_age[23]]
# Check true
if validate_age[23]:
print["validate_age function returns True"]
else:
print["validate_age function returns False"]
Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về mã chức năng Python này.
Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 [Phiên bản cộng đồng] IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.
Làm thế nào để bạn kiểm tra xem một cái gì đó là
if user_can_read[request.user, b]:
## do stuff
3 trong Python? Có ba cách:- Một cách "xấu":
4if user_can_read[request.user, b]: ## do stuff
- Một cách khác "xấu":
5if user_can_read[request.user, b]: ## do stuff
- Và cách tốt, được đề xuất ngay cả trong các khuyến nghị lập trình của PEP8:
6if user_can_read[request.user, b]: ## do stuff
Những cách "xấu" không chỉ được cau mày mà còn chậm hơn. Hãy sử dụng một bài kiểm tra đơn giản:
$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
Sử dụng
if user_can_read[request.user, b]:
## do stuff
7 chậm hơn khoảng 60% so với if user_can_read[request.user, b]:
## do stuff
8 [17,4/10,9≈1.596], nhưng sử dụng if user_can_read[request.user, b]:
## do stuff
9 chậm hơn 120% [24,9/10,9≈2.284]! Không quan trọng nếu if function_Name[argu]:
# do_something
0 thực sự là if user_can_read[request.user, b]:
## do stuff
3 hoặc if user_can_read[request.user, b]:
## do stuff
0 - sự khác biệt về hiệu suất là tương tự [nếu if function_Name[argu]:
# do_something
0 là if user_can_read[request.user, b]:
## do stuff
3, cả ba kịch bản sẽ chậm hơn một chút].Tương tự, chúng ta có thể kiểm tra xem một biến không phải là
if user_can_read[request.user, b]:
## do stuff
3 bằng cách sử dụng một trong các phương pháp sau:
6 ["xấu"]if function_Name[argu]: # do_something
7 ["xấu"]if function_Name[argu]: # do_something
8 [tốt]if function_Name[argu]: # do_something
$ python -m timeit -s "variable=False" "if variable != True: pass"
10000000 loops, best of 5: 26 nsec per loop$ python -m timeit -s "variable=False" "if variable is not True: pass"
10000000 loops, best of 5: 18.8 nsec per loop
$ python -m timeit -s "variable=False" "if not variable: pass"
20000000 loops, best of 5: 12.4 nsec per loop
if function_Name[argu]:
# do_something
9 thắng. def validate_age[age]:
if age > 20:
return True
else:
return False
# Print value
print[validate_age[23]]
# Check true
if validate_age[23]:
print["validate_age function returns True"]
else:
print["validate_age function returns False"]
0 chậm hơn 50% [18,8/12.4≈1.516] và def validate_age[age]:
if age > 20:
return True
else:
return False
# Print value
print[validate_age[23]]
# Check true
if validate_age[23]:
print["validate_age function returns True"]
else:
print["validate_age function returns False"]
1 mất gấp đôi thời gian [26/12.4≈2.016].Các phiên bản
if user_can_read[request.user, b]:
## do stuff
8 và if function_Name[argu]:
# do_something
9 nhanh hơn để thực hiện và nhanh hơn để đọc. Chúng là những thành ngữ phổ biến mà bạn thường thấy trong Python [hoặc các ngôn ngữ lập trình khác].Về loạt bài "Viết Python nhanh hơn"
"Viết Python nhanh hơn" là một loạt các bài viết ngắn thảo luận về cách giải quyết một số vấn đề phổ biến với các cấu trúc mã khác nhau. Tôi chạy một số điểm chuẩn, thảo luận về sự khác biệt giữa mỗi đoạn mã và kết thúc với một số khuyến nghị cá nhân.
Những khuyến nghị đó sẽ làm cho mã của bạn nhanh hơn nhiều? Không thực sự. Có phải biết những khác biệt nhỏ đó sẽ tạo ra một lập trình viên Python tốt hơn một chút? Hy vọng!
Is knowing those small differences going to make a slightly better Python programmer? Hopefully!
Bạn có thể đọc thêm về một số giả định tôi đã thực hiện, thiết lập điểm chuẩn và câu trả lời cho một số câu hỏi phổ biến trong bài viết giới thiệu.
"Sự thật" và "Falsy"
Tại sao tôi tiếp tục đưa "xấu" vào báo giá? Đó là bởi vì cách "xấu" không phải lúc nào cũng xấu [điều đó chỉ sai khi bạn muốn so sánh các giá trị Boolean, như được chỉ trong PEP8]. Đôi khi, bạn cố tình phải sử dụng một trong những so sánh khác.
Trong Python [và nhiều ngôn ngữ khác], có
if user_can_read[request.user, b]:
## do stuff
3, và có những giá trị sự thật. Nghĩa là, các giá trị được hiểu là if user_can_read[request.user, b]:
## do stuff
3 nếu bạn chạy def validate_age[age]:
if age > 20:
return True
else:
return False
# Print value
print[validate_age[23]]
# Check true
if validate_age[23]:
print["validate_age function returns True"]
else:
print["validate_age function returns False"]
6. Tương tự, có if user_can_read[request.user, b]:
## do stuff
0 và có các giá trị giả [giá trị trả về if user_can_read[request.user, b]:
## do stuff
0 từ def validate_age[age]:
if age > 20:
return True
else:
return False
# Print value
print[validate_age[23]]
# Check true
if validate_age[23]:
print["validate_age function returns True"]
else:
print["validate_age function returns False"]
6]. Một danh sách trống [$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
0], chuỗi [$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
1], từ điển [$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
2], $ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
3 và 0 đều giả mạo nhưng chúng không hoàn toàn if user_can_read[request.user, b]:
## do stuff
0.Đôi khi bạn cần phân biệt giữa ____ 13/________ 10 và các giá trị sự thật/giả. Nếu mã của bạn sẽ hoạt động theo một cách khi bạn vượt qua một danh sách trống và trong một danh sách khác, khi bạn vượt qua ____10, bạn không thể sử dụng
$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
8.Hãy xem kịch bản sau:
def process_orders[orders=None]:
if not orders:
# There are no orders, return
return
else:
# Process orders
...
Chúng tôi có một chức năng để xử lý một số đơn đặt hàng. Nếu không có đơn đặt hàng, chúng tôi muốn quay lại mà không làm gì cả. Nếu không, chúng tôi muốn xử lý các đơn đặt hàng hiện có.
Chúng tôi giả định rằng nếu không có đơn đặt hàng, thì tham số
$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
9 được đặt thành $ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
3. Nhưng, nếu $ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
9 là một danh sách trống, chúng tôi cũng quay lại mà không có bất kỳ hành động nào! Và có lẽ có thể nhận được một danh sách trống vì ai đó chỉ cập nhật thông tin thanh toán của một đơn đặt hàng trong quá khứ? Hoặc có lẽ có một danh sách trống có nghĩa là có một lỗi trong hệ thống. Chúng ta nên bắt lỗi đó trước khi chúng ta điền vào cơ sở dữ liệu với các đơn đặt hàng trống! Bất kể lý do gì cho một danh sách trống, mã trên sẽ bỏ qua nó. Chúng tôi có thể sửa nó bằng cách điều tra tham số $ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
9 cẩn thận hơn:def process_orders[orders=None]:
if orders is None:
# orders is None, return
return
elif orders == []:
# Process empty list of orders
...
elif len[orders] > 0:
# Process existing orders
...
Điều tương tự áp dụng cho các giá trị sự thật. Nếu mã của bạn sẽ hoạt động khác với
if user_can_read[request.user, b]:
## do stuff
3 so với, giả sử, giá trị $ python -m timeit -s "variable=False" "if variable != True: pass"
10000000 loops, best of 5: 26 nsec per loop$ python -m timeit -s "variable=False" "if variable is not True: pass"
10000000 loops, best of 5: 18.8 nsec per loop
$ python -m timeit -s "variable=False" "if not variable: pass"
20000000 loops, best of 5: 12.4 nsec per loop
4, chúng tôi không thể sử dụng if user_can_read[request.user, b]:
## do stuff
8. Chúng ta nên sử dụng if user_can_read[request.user, b]:
## do stuff
9 để so sánh số [$ python -m timeit -s "variable=False" "if variable != True: pass"
10000000 loops, best of 5: 26 nsec per loop$ python -m timeit -s "variable=False" "if variable is not True: pass"
10000000 loops, best of 5: 18.8 nsec per loop
$ python -m timeit -s "variable=False" "if not variable: pass"
20000000 loops, best of 5: 12.4 nsec per loop
7] và if user_can_read[request.user, b]:
## do stuff
7 để so sánh với if user_can_read[request.user, b]:
## do stuff
3 [def process_orders[orders=None]:
if not orders:
# There are no orders, return
return
else:
# Process orders
...
0]. Nghe có vẻ khó hiểu? Chúng ta hãy xem sự khác biệt giữa if user_can_read[request.user, b]:
## do stuff
7 và if user_can_read[request.user, b]:
## do stuff
9.if user_can_read[request.user, b]:
## do stuff
7 Kiểm tra danh tính, if user_can_read[request.user, b]:
## do stuff
9 kiểm tra giá trị
if user_can_read[request.user, b]:
## do stuff
if user_can_read[request.user, b]:
## do stuff
Toán tử
if user_can_read[request.user, b]:
## do stuff
7 so sánh danh tính của các đối tượng. Nếu hai biến giống hệt nhau, điều đó có nghĩa là chúng chỉ vào cùng một đối tượng [cùng một vị trí trong bộ nhớ]. Cả hai đều có cùng một ID [mà bạn có thể kiểm tra với hàm ID []].Toán tử
if user_can_read[request.user, b]:
## do stuff
9 so sánh các giá trị. Nó kiểm tra xem giá trị của một biến bằng với giá trị của một số biến khác.Một số đối tượng trong Python là duy nhất, như
$ python -m timeit -s "variable=False" "if variable == True: pass"
10000000 loops, best of 5: 24.9 nsec per loop$ python -m timeit -s "variable=False" "if variable is True: pass"
10000000 loops, best of 5: 17.4 nsec per loop
$ python -m timeit -s "variable=False" "if variable: pass"
20000000 loops, best of 5: 10.9 nsec per loop
3, if user_can_read[request.user, b]:
## do stuff
3 hoặc if user_can_read[request.user, b]:
## do stuff
0. Mỗi lần bạn gán một biến cho if user_can_read[request.user, b]:
## do stuff
3, nó chỉ vào cùng một đối tượng if user_can_read[request.user, b]:
## do stuff
3 như các biến khác được gán cho if user_can_read[request.user, b]:
## do stuff
3. Nhưng mỗi lần bạn tạo một danh sách mới, Python tạo ra một đối tượng mới:>>> a = True
>>> b = True
>>> a is b
True
# Variables that are identical are always also equal!
>>> a == b
True# But
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False # Those lists are two different objects
>>> a == b
True # Both lists are equal [contain the same elements]
Điều quan trọng là phải biết sự khác biệt giữa
if user_can_read[request.user, b]:
## do stuff
7 và if user_can_read[request.user, b]:
## do stuff
9. Nếu bạn nghĩ rằng chúng hoạt động giống nhau, bạn có thể kết thúc với những lỗi kỳ lạ trong mã của bạn:a = 1
# This will print 'yes'
if a is 1:
print['yes']b = 1000
# This won't!
if b is 1000:
print['yes']
Trong ví dụ trên, khối mã đầu tiên sẽ in "Có", nhưng lần thứ hai sẽ không. Đó là bởi vì Python thực hiện một số tối ưu hóa nhỏ và các số nguyên nhỏ có chung ID [chúng chỉ vào cùng một đối tượng]. Mỗi lần bạn gán
$ python -m timeit -s "variable=False" "if variable != True: pass"
10000000 loops, best of 5: 26 nsec per loop$ python -m timeit -s "variable=False" "if variable is not True: pass"
10000000 loops, best of 5: 18.8 nsec per loop
$ python -m timeit -s "variable=False" "if not variable: pass"
20000000 loops, best of 5: 12.4 nsec per loop
4 cho một biến mới, nó chỉ vào cùng một đối tượng $ python -m timeit -s "variable=False" "if variable != True: pass"
10000000 loops, best of 5: 26 nsec per loop$ python -m timeit -s "variable=False" "if variable is not True: pass"
10000000 loops, best of 5: 18.8 nsec per loop
$ python -m timeit -s "variable=False" "if not variable: pass"
20000000 loops, best of 5: 12.4 nsec per loop
4. Nhưng khi bạn gán def process_orders[orders=None]:
if orders is None:
# orders is None, return
return
elif orders == []:
# Process empty list of orders
...
elif len[orders] > 0:
# Process existing orders
...
7 cho một biến, nó sẽ tạo ra một đối tượng mới. Nếu chúng ta sử dụng def process_orders[orders=None]:
if orders is None:
# orders is None, return
return
elif orders == []:
# Process empty list of orders
...
elif len[orders] > 0:
# Process existing orders
...
8, thì mọi thứ sẽ hoạt động như mong đợi.Kết luận
Tóm lại:
- Để kiểm tra xem một biến có bằng đúng/sai không [và bạn không phải phân biệt giữa ____ 13/________ 10 và các giá trị sự thật/giả], sử dụng
8 hoặcif user_can_read[request.user, b]: ## do stuff
9. Đó là cách đơn giản và nhanh nhất để làm điều này.if function_Name[argu]: # do_something
- Nếu bạn muốn kiểm tra xem một biến có đúng hay sai không [và không phải là sự thật/giả], hãy sử dụng
7 [if user_can_read[request.user, b]: ## do stuff
0].is explicitly True or False [and is not truthy/falsy], usedef process_orders[orders=None]:
if not orders:
# There are no orders, return
return
else:
# Process orders
...
7 [if user_can_read[request.user, b]: ## do stuff
0].def process_orders[orders=None]:
if not orders:
# There are no orders, return
return
else:
# Process orders
... - Nếu bạn muốn kiểm tra xem một biến có bằng 0 hoặc nếu danh sách trống, hãy sử dụng
5 hoặc>>> a = True
>>> b = True
>>> a is b
True
# Variables that are identical are always also equal!
>>> a == b
True# But
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False # Those lists are two different objects
>>> a == b
True # Both lists are equal [contain the same elements]
6.>>> a = True
>>> b = True
>>> a is b
True
# Variables that are identical are always also equal!
>>> a == b
True# But
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a is b
False # Those lists are two different objects
>>> a == b
True # Both lists are equal [contain the same elements]