>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
4 trong python là một công cụ mạnh mẽ để tìm tất cả các kết hợp của thuật ngữ r, tuy nhiên, tôi muốn biết về độ phức tạp tính toán của nóGiả sử tôi muốn biết độ phức tạp của n và r, và chắc chắn nó sẽ cho tôi tất cả tổ hợp thuật ngữ r từ danh sách n thuật ngữ
Theo tài liệu chính thức, đây là triển khai sơ bộ
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
Giải pháp tốt nhất
Tôi có thể nói nó là
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
5, phần >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
6 là số lần bộ tạo phải >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7 và cũng là số lần vòng lặp bên ngoài của >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
8Trong mỗi lần lặp lại, ít nhất bộ dữ liệu đầu ra có độ dài
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
9 cần được tạo, điều này mang lại hệ số bổ sung >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
9. Các vòng lặp bên trong khác cũng sẽ là >>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
1 cho mỗi lần lặp bên ngoàiĐiều này giả định rằng việc tạo bộ dữ liệu thực sự là
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
1 và danh sách get/set thực sự là >>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
3 ở mức trung bình ít nhất với mẫu truy cập cụ thể trong thuật toán. Nếu đây không phải là trường hợp, thì vẫn >>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
4 mặc dùNhư thường lệ trong loại phân tích này, tôi giả sử tất cả các phép toán số nguyên là
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
3 ngay cả khi kích thước của chúng không bị giới hạnGiải pháp liên quan
Python – Siêu dữ liệu trong Python là gì
Các lớp như các đối tượng
Trước khi hiểu về siêu dữ liệu, bạn cần nắm vững các lớp trong Python. Và Python có một ý tưởng rất đặc biệt về lớp là gì, mượn từ ngôn ngữ Smalltalk
Trong hầu hết các ngôn ngữ, các lớp chỉ là những đoạn mã mô tả cách tạo ra một đối tượng. Điều đó cũng đúng trong Python
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
3Nhưng các lớp còn nhiều hơn thế trong Python. Lớp cũng là đối tượng
Vâng, đồ vật
Ngay khi bạn sử dụng từ khóa
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
6, Python sẽ thực thi nó và tạo một đối tượng. Hướng dẫndef combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
5tạo trong bộ nhớ một đối tượng có tên
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
7Bản thân đối tượng này [lớp] có khả năng tạo đối tượng [các thể hiện] và đây là lý do tại sao nó là một lớp
Tuy nhiên, nó vẫn là một đối tượng, và do đó
- bạn có thể gán nó cho một biến
- bạn có thể sao chép nó
- bạn có thể thêm các thuộc tính cho nó
- bạn có thể chuyển nó dưới dạng tham số chức năng
e. g
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
7Tạo lớp độngVì các lớp là các đối tượng, bạn có thể tạo chúng một cách nhanh chóng, giống như bất kỳ đối tượng nào
Đầu tiên, bạn có thể tạo một lớp trong một hàm bằng cách sử dụng
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
6>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
Nhưng nó không năng động lắm, vì bạn vẫn phải tự viết cả lớp
Vì các lớp là các đối tượng, chúng phải được tạo bởi một cái gì đó
Khi bạn sử dụng từ khóa
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
6, Python sẽ tự động tạo đối tượng này. Nhưng cũng giống như hầu hết mọi thứ trong Python, nó cung cấp cho bạn một cách để thực hiện thủ côngGhi chức năng
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10? ________số 8Chà, có một khả năng hoàn toàn khác, nó cũng có thể tạo các lớp một cách nhanh chóng.
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 có thể lấy mô tả của một lớp làm tham số và trả về một lớp[Tôi biết, thật ngớ ngẩn khi cùng một chức năng có thể có hai cách sử dụng hoàn toàn khác nhau tùy theo các tham số bạn truyền cho nó. Đó là sự cố do khả năng tương thích ngược trong Python]
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 hoạt động theo cách này>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
1Ở đâu
14. tên của lớp>>> def choose_class[name]: .. if name == 'foo': .. class Foo[object]: .. pass .. return Foo # return the class, not an instance .. else: .. class Bar[object]: .. pass .. return Bar ... >>> MyClass = choose_class['foo'] >>> print[MyClass] # the function returns a class, not an instance >>> print[MyClass[]] # you can create an object from this class
15. bộ của lớp cha [để kế thừa, có thể để trống]>>> def choose_class[name]: .. if name == 'foo': .. class Foo[object]: .. pass .. return Foo # return the class, not an instance .. else: .. class Bar[object]: .. pass .. return Bar ... >>> MyClass = choose_class['foo'] >>> print[MyClass] # the function returns a class, not an instance >>> print[MyClass[]] # you can create an object from this class
16. từ điển chứa tên và giá trị thuộc tính>>> def choose_class[name]: .. if name == 'foo': .. class Foo[object]: .. pass .. return Foo # return the class, not an instance .. else: .. class Bar[object]: .. pass .. return Bar ... >>> MyClass = choose_class['foo'] >>> print[MyClass] # the function returns a class, not an instance >>> print[MyClass[]] # you can create an object from this class
e. g
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
9có thể được tạo thủ công theo cách này
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
0Bạn sẽ nhận thấy rằng chúng tôi sử dụng
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
17 làm tên của lớp và làm biến để giữ tham chiếu lớp. Chúng có thể khác nhau, nhưng không có lý do gì để phức tạp hóa mọi thứ>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 chấp nhận một từ điển để định nghĩa các thuộc tính của lớp. Cho nên>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
3Có thể được dịch sang
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
30Và được sử dụng như một lớp bình thường
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
31Và tất nhiên, bạn có thể kế thừa từ nó, vì vậy
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
32sẽ là
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
33Cuối cùng, bạn sẽ muốn thêm các phương thức vào lớp của mình. Chỉ cần xác định một hàm có chữ ký phù hợp và gán nó làm thuộc tính
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
34Và bạn có thể thêm nhiều phương thức hơn nữa sau khi bạn tự động tạo lớp, giống như thêm các phương thức vào một đối tượng lớp được tạo bình thường
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
35Bạn thấy chúng ta đang đi đâu. trong Python, các lớp là các đối tượng và bạn có thể tạo một lớp nhanh chóng, linh hoạt
Đây là những gì Python làm khi bạn sử dụng từ khóa
>>> print[type[1]]
>>> print[type["1"]]
>>> print[type[ObjectCreator]]
>>> print[type[ObjectCreator[]]]
6 và nó làm như vậy bằng cách sử dụng siêu dữ liệuSiêu dữ liệu là gì [cuối cùng]Siêu dữ liệu là 'công cụ' tạo ra các lớp
Bạn định nghĩa các lớp để tạo các đối tượng, phải không?
Nhưng chúng tôi đã học được rằng các lớp Python là các đối tượng
Chà, siêu dữ liệu là thứ tạo ra các đối tượng này. Chúng là các lớp của các lớp, bạn có thể hình dung chúng theo cách này
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
36Bạn đã thấy rằng
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 cho phép bạn làm điều gì đó như thế nàydef combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
37Đó là bởi vì hàm
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 trên thực tế là một siêu dữ liệu. >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 là siêu dữ liệu Python sử dụng để tạo tất cả các lớp đằng sau hậu trườngBây giờ bạn tự hỏi "tại sao nó lại được viết bằng chữ thường mà không phải là
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
93?"Chà, tôi đoán đó là vấn đề nhất quán với
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
94, lớp tạo đối tượng chuỗi và >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
95 lớp tạo đối tượng số nguyên. >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 chỉ là lớp tạo các đối tượng lớpBạn thấy điều đó bằng cách kiểm tra thuộc tính
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
97Mọi thứ, và ý tôi là mọi thứ, là một đối tượng trong Python. Điều đó bao gồm số nguyên, chuỗi, hàm và lớp. Tất cả đều là đối tượng. Và tất cả chúng đã được tạo ra từ một lớp
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
38Bây giờ,
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
97 của bất kỳ >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
97 nào?def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
39Vì vậy, siêu dữ liệu chỉ là thứ tạo ra các đối tượng lớp
Bạn có thể gọi nó là 'nhà máy đẳng cấp' nếu muốn
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 là siêu dữ liệu tích hợp sẵn mà Python sử dụng, nhưng tất nhiên, bạn có thể tạo siêu dữ liệu của riêng mìnhthuộc tínhTrong Python 2, bạn có thể thêm thuộc tính
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 khi viết một lớp [xem phần tiếp theo để biết cú pháp Python 3]def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
50Nếu bạn làm như vậy, Python sẽ sử dụng siêu dữ liệu để tạo lớp
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
03Cẩn thận, nó khó
Bạn viết
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
04 trước, nhưng đối tượng lớp >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
03 chưa được tạo trong bộ nhớPython sẽ tìm kiếm
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 trong định nghĩa lớp. Nếu tìm thấy, nó sẽ sử dụng nó để tạo lớp đối tượng >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
03. Nếu không, nó sẽ sử dụng >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 để tạo lớpĐọc cái đó mấy lần
khi bạn làm
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
51Python làm như sau
Có thuộc tính
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 trong >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
03 không?Nếu có, hãy tạo trong bộ nhớ một đối tượng lớp [tôi đã nói là một đối tượng lớp, hãy ở lại với tôi ở đây], với tên
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
03 bằng cách sử dụng những gì có trong >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01Nếu Python không thể tìm thấy
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01, nó sẽ tìm kiếm một >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 ở cấp độ MODULE và cố gắng làm điều tương tự [nhưng chỉ đối với các lớp không kế thừa bất kỳ thứ gì, về cơ bản là các lớp kiểu cũ]Sau đó, nếu nó không thể tìm thấy bất kỳ
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 nào, nó sẽ sử dụng siêu dữ liệu riêng của >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
36 [cha đầu tiên] [có thể là >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 mặc định] để tạo đối tượng lớpHãy cẩn thận ở đây rằng thuộc tính
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 sẽ không được kế thừa, siêu dữ liệu của cha mẹ [>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
39] sẽ là. Nếu >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
36 đã sử dụng thuộc tính >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 đã tạo >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
36 với def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
303 [chứ không phải def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
304], các lớp con sẽ không kế thừa hành vi đóBây giờ câu hỏi lớn là, bạn có thể đặt gì vào
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01?Câu trả lời là một cái gì đó có thể tạo ra một lớp học
Và những gì có thể tạo ra một lớp học?
Siêu dữ liệu trong Python 3Cú pháp đặt siêu dữ liệu đã được thay đổi trong Python 3
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
52i. e. thuộc tính
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 không còn được sử dụng, thay vào đó là đối số từ khóa trong danh sách các lớp cơ sởTuy nhiên, hành vi của siêu dữ liệu hầu như không thay đổi
Một điều được thêm vào siêu dữ liệu trong Python 3 là bạn cũng có thể chuyển các thuộc tính dưới dạng đối số từ khóa vào siêu dữ liệu, như vậy
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
53Đọc phần bên dưới để biết cách Python xử lý việc này
Siêu dữ liệu tùy chỉnhMục đích chính của siêu dữ liệu là tự động thay đổi lớp khi nó được tạo
Bạn thường làm điều này cho các API, nơi bạn muốn tạo các lớp phù hợp với ngữ cảnh hiện tại
Hãy tưởng tượng một ví dụ ngớ ngẩn, nơi bạn quyết định rằng tất cả các lớp trong mô-đun của bạn phải có các thuộc tính được viết hoa. Có một số cách để thực hiện việc này, nhưng một cách là đặt
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 ở cấp độ mô-đunBằng cách này, tất cả các lớp của mô-đun này sẽ được tạo bằng cách sử dụng siêu dữ liệu này và chúng ta chỉ cần yêu cầu siêu dữ liệu chuyển tất cả các thuộc tính thành chữ hoa
May mắn thay,
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 thực sự có thể gọi được bất kỳ, nó không cần phải là một lớp chính thức [tôi biết, thứ gì đó có 'lớp' trong tên của nó không cần phải là một lớp, hãy xem. nhưng nó hữu ích]Vì vậy, chúng ta sẽ bắt đầu với một ví dụ đơn giản, bằng cách sử dụng hàm
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
54Hãy kiểm tra
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
55Bây giờ, hãy làm giống hệt như vậy, nhưng sử dụng một lớp thực cho một siêu dữ liệu
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
56Bây giờ chúng ta hãy viết lại những điều trên, nhưng với các tên biến ngắn hơn và thực tế hơn khi chúng ta biết ý nghĩa của chúng
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
57Bạn có thể đã nhận thấy đối số bổ sung
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
310. Không có gì đặc biệt về nó. def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
311 luôn nhận lớp mà nó được định nghĩa, làm tham số đầu tiên. Giống như bạn có def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
312 cho các phương thức thông thường nhận thể hiện làm tham số đầu tiên hoặc lớp xác định cho các phương thức lớpNhưng điều này không đúng OOP. Chúng tôi đang gọi trực tiếp cho ____210 và chúng tôi không ghi đè hoặc gọi cho ____1311 của phụ huynh. Hãy làm điều đó thay vào đó
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
58Chúng ta có thể làm cho nó sạch hơn nữa bằng cách sử dụng
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
315, điều này sẽ giúp dễ dàng thừa kế [vì có, bạn có thể có siêu dữ liệu, kế thừa từ siêu dữ liệu, kế thừa từ loại]def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
59Ồ, và trong Python 3 nếu bạn thực hiện cuộc gọi này với các đối số từ khóa, như thế này
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
70Nó chuyển thành cái này trong siêu dữ liệu để sử dụng nó
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
71Đó là nó. Thực sự không có gì hơn về siêu dữ liệu
Lý do đằng sau sự phức tạp của mã khi sử dụng siêu dữ liệu không phải là do siêu dữ liệu, mà là vì bạn thường sử dụng siêu dữ liệu để thực hiện những thứ phức tạp dựa trên nội quan, thao tác thừa kế, các vars như
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
316, v.v.Thật vậy, siêu dữ liệu đặc biệt hữu ích để thực hiện ma thuật đen, và do đó, những thứ phức tạp. Nhưng bản thân chúng thì đơn giản
- chặn việc tạo lớp
- sửa đổi lớp
- trả lại lớp đã sửa đổi
Vì
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 có thể chấp nhận bất kỳ cuộc gọi nào, tại sao bạn lại sử dụng một lớp vì nó rõ ràng là phức tạp hơn?Có một số lý do để làm như vậy
- Ý định rõ ràng. Khi bạn đọc
318, bạn sẽ biết điều gì sẽ xảy ra tiếp theodef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
- Bạn có thể sử dụng OOP. Siêu dữ liệu có thể kế thừa từ siêu dữ liệu, ghi đè các phương thức cha. Siêu dữ liệu thậm chí có thể sử dụng siêu dữ liệu
- Các lớp con của một lớp sẽ là thể hiện của siêu dữ liệu của nó nếu bạn đã chỉ định một lớp siêu dữ liệu, nhưng không phải với hàm siêu dữ liệu
- Bạn có thể cấu trúc mã của mình tốt hơn. Bạn không bao giờ sử dụng siêu dữ liệu cho những thứ tầm thường như ví dụ trên. Nó thường dành cho một cái gì đó phức tạp. Có khả năng tạo một số phương thức và nhóm chúng trong một lớp rất hữu ích để làm cho mã dễ đọc hơn
- Bạn có thể kết nối với
311,def combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
320 vàdef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
321. Điều này sẽ cho phép bạn làm những việc khác nhau, Ngay cả khi thông thường bạn có thể làm tất cả trongdef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
311, một số người vẫn cảm thấy thoải mái hơn khi sử dụngdef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
320def combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
- Chúng được gọi là siêu dữ liệu, chết tiệt. Nó phải có ý nghĩa gì đó
Bây giờ câu hỏi lớn. Tại sao bạn lại sử dụng một số tính năng dễ bị lỗi tối nghĩa?
Chà, thường thì bạn không
Siêu dữ liệu là phép thuật sâu sắc hơn mà 99% người dùng không bao giờ phải lo lắng về điều đó. Nếu bạn tự hỏi liệu bạn có cần chúng hay không, thì bạn không cần [những người thực sự cần chúng biết chắc chắn rằng họ cần chúng và không cần lời giải thích tại sao]
Python Guru Tim Peters
Trường hợp sử dụng chính cho siêu dữ liệu là tạo API. Một ví dụ điển hình của điều này là Django ORM. Nó cho phép bạn xác định một cái gì đó như thế này
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
72Nhưng nếu bạn làm điều này
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
73Nó sẽ không trả về một đối tượng
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
324. Nó sẽ trả về một >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
95 và thậm chí có thể lấy nó trực tiếp từ cơ sở dữ liệuĐiều này là có thể bởi vì
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
326 định nghĩa >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
01 và nó sử dụng một số phép thuật sẽ biến def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
328 mà bạn vừa xác định bằng các câu lệnh đơn giản thành một móc nối phức tạp với trường cơ sở dữ liệuDjango làm cho thứ gì đó phức tạp trở nên đơn giản bằng cách hiển thị một API đơn giản và sử dụng siêu dữ liệu, tạo lại mã từ API này để thực hiện công việc thực sự đằng sau hậu trường
Lời cuốiĐầu tiên, bạn biết rằng các lớp là các đối tượng có thể tạo các thể hiện
Chà, trên thực tế, bản thân các lớp là các thể hiện. Của siêu dữ liệu
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
74Mọi thứ đều là một đối tượng trong Python và tất cả chúng đều là thể hiện của lớp hoặc thể hiện của siêu dữ liệu
Ngoại trừ
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
10 thực sự là siêu dữ liệu của chính nó. Đây không phải là thứ bạn có thể sao chép bằng Python thuần túy và được thực hiện bằng cách gian lận một chút ở cấp độ triển khaiThứ hai, siêu dữ liệu phức tạp. Bạn có thể không muốn sử dụng chúng để thay đổi lớp rất đơn giản. Bạn có thể thay đổi các lớp bằng cách sử dụng hai kỹ thuật khác nhau
- khỉ vá
- trang trí lớp học
99% thời gian bạn cần thay đổi lớp, tốt hơn hết bạn nên sử dụng những thứ này
Nhưng 98% thời gian, bạn hoàn toàn không cần thay đổi lớp học
Python – Từ khóa “yield” làm gì
Để hiểu những gì
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7 làm, bạn phải hiểu máy phát điện là gì. Và trước khi bạn có thể hiểu về máy phát điện, bạn phải hiểu về iterablesIterables
Khi bạn tạo một danh sách, bạn có thể đọc từng mục một. Đọc từng mục của nó được gọi là lặp lại
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
75def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
332 là một lần lặp. Khi bạn sử dụng khả năng hiểu danh sách, bạn tạo một danh sách và do đó, một danh sách có thể lặp lạidef combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
76Mọi thứ bạn có thể sử dụng "
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
333" trên đều có thể lặp lại; Các lần lặp này rất tiện lợi vì bạn có thể đọc chúng bao nhiêu tùy thích, nhưng bạn lưu trữ tất cả các giá trị trong bộ nhớ và điều này không phải lúc nào bạn cũng muốn khi bạn có nhiều giá trị
máy phát điện
Trình tạo là trình lặp, một loại có thể lặp lại mà bạn chỉ có thể lặp lại một lần. Trình tạo không lưu trữ tất cả các giá trị trong bộ nhớ, chúng tạo ra các giá trị một cách nhanh chóng
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
77Nó giống nhau ngoại trừ bạn đã sử dụng
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
336 thay vì def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
337. NHƯNG, bạn không thể thực hiện def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
338 lần thứ hai vì máy phát điện chỉ có thể được sử dụng một lần. họ tính 0, sau đó quên nó đi và tính 1, và cuối cùng tính 4, từng người mộtnăng suất
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7 là một từ khóa được sử dụng giống như def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
340, ngoại trừ hàm sẽ trả về một trình tạodef combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
78Đây là một ví dụ vô ích, nhưng sẽ rất hữu ích khi bạn biết hàm của mình sẽ trả về một tập hợp giá trị khổng lồ mà bạn chỉ cần đọc một lần
Để thành thạo
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7, bạn phải hiểu rằng khi bạn gọi hàm, mã bạn đã viết trong thân hàm không chạy. Hàm chỉ trả về đối tượng trình tạo, điều này hơi phức tạpSau đó, mã của bạn sẽ tiếp tục từ nơi nó dừng lại mỗi khi
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
342 sử dụng trình tạoBây giờ là phần khó khăn
Lần đầu tiên
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
342 gọi đối tượng trình tạo được tạo từ hàm của bạn, nó sẽ chạy mã trong hàm của bạn ngay từ đầu cho đến khi nó chạm vào >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7, sau đó nó sẽ trả về giá trị đầu tiên của vòng lặp. Sau đó, mỗi lệnh gọi tiếp theo sẽ chạy một lần lặp khác của vòng lặp mà bạn đã viết trong hàm và trả về giá trị tiếp theo. Điều này sẽ tiếp tục cho đến khi trình tạo được coi là trống, điều này xảy ra khi chức năng chạy mà không nhấn >>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
7. Đó có thể là do vòng lặp đã kết thúc hoặc do bạn không còn thỏa mãn một def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
346mã của bạn giải thích
Máy phát điện
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
79người gọi
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
0Mã này chứa một số phần thông minh
Vòng lặp lặp lại trên một danh sách, nhưng danh sách mở rộng trong khi vòng lặp đang được lặp lại. Đó là một cách ngắn gọn để xem qua tất cả các dữ liệu lồng nhau này ngay cả khi nó hơi nguy hiểm vì bạn có thể kết thúc bằng một vòng lặp vô hạn. Trong trường hợp này,
347 làm cạn kiệt tất cả các giá trị của trình tạo, nhưngdef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
8 tiếp tục tạo các đối tượng trình tạo mới sẽ tạo ra các giá trị khác với các giá trị trước đó do nó không được áp dụng trên cùng một nút>>> def choose_class[name]: .. if name == 'foo': .. class Foo[object]: .. pass .. return Foo # return the class, not an instance .. else: .. class Bar[object]: .. pass .. return Bar ... >>> MyClass = choose_class['foo'] >>> print[MyClass] # the function returns a class, not an instance >>> print[MyClass[]] # you can create an object from this class
Phương thức
349 là một phương thức đối tượng danh sách mong đợi một lần lặp và thêm các giá trị của nó vào danh sáchdef combinations[iterable, r]: # combinations['ABCD', 2] --> AB AC AD BC BD CD # combinations[range[4], 3] --> 012 013 023 123 pool = tuple[iterable] n = len[pool] if r > n: return indices = list[range[r]] yield tuple[pool[i] for i in indices] while True: for i in reversed[range[r]]: if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range[i+1, r]: indices[j] = indices[j-1] + 1 yield tuple[pool[i] for i in indices]
Thông thường chúng ta chuyển một danh sách cho nó
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
1Nhưng trong mã của bạn, nó có một trình tạo, điều này tốt bởi vì
- Bạn không cần phải đọc các giá trị hai lần
- Bạn có thể có rất nhiều con và bạn không muốn tất cả chúng được lưu trữ trong bộ nhớ
Và nó hoạt động vì Python không quan tâm liệu đối số của phương thức có phải là danh sách hay không. Python mong đợi các lần lặp để nó sẽ hoạt động với các chuỗi, danh sách, bộ dữ liệu và trình tạo. Đây được gọi là cách gõ vịt và là một trong những lý do tại sao Python rất tuyệt. Nhưng đây là một câu chuyện khác, cho một câu hỏi khác
Bạn có thể dừng tại đây hoặc đọc một chút để xem cách sử dụng nâng cao của trình tạo
Kiểm soát cạn kiệt máy phát điện
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
2Ghi chú. Đối với Python 3, sử dụng____1350 hoặc
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
351Nó có thể hữu ích cho nhiều thứ khác nhau như kiểm soát quyền truy cập vào tài nguyên
Itertools, người bạn tốt nhất của bạn
Mô-đun itertools chứa các chức năng đặc biệt để thao tác với các lần lặp. Bạn đã bao giờ muốn sao chép một máy phát điện chưa?
Vậy thì chỉ cần
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
353Một ví dụ?
>>> def choose_class[name]:
.. if name == 'foo':
.. class Foo[object]:
.. pass
.. return Foo # return the class, not an instance
.. else:
.. class Bar[object]:
.. pass
.. return Bar
...
>>> MyClass = choose_class['foo']
>>> print[MyClass] # the function returns a class, not an instance
>>> print[MyClass[]] # you can create an object from this class
3Hiểu các cơ chế bên trong của phép lặp
Lặp lại là một quá trình ngụ ý có thể lặp lại [triển khai phương thức
def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
354] và trình vòng lặp [triển khai phương thức def combinations[iterable, r]:
# combinations['ABCD', 2] --> AB AC AD BC BD CD
# combinations[range[4], 3] --> 012 013 023 123
pool = tuple[iterable]
n = len[pool]
if r > n:
return
indices = list[range[r]]
yield tuple[pool[i] for i in indices]
while True:
for i in reversed[range[r]]:
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range[i+1, r]:
indices[j] = indices[j-1] + 1
yield tuple[pool[i] for i in indices]
355]. Iterables là bất kỳ đối tượng nào bạn có thể lấy một iterator từ. Iterators là đối tượng cho phép bạn lặp lại trên iterables