Cập nhật: Chỉ cần thấy tham chiếu đến
a = A[]
method = a.method1
method[1, 2]
5 trong bài viết của bạn. Điều đó khác. Sử dụng a = A[]
method = a.method1
method[1, 2]
6 để lấy đối tượng chức năng và sau đó gọi nó bằng các đối số của bạnclass A[object]:
def method1[self, a, b, c]:
# foo
method = A.method1
a = A[]
method = a.method1
method[1, 2]
7 hiện là một đối tượng chức năng thực tế. mà bạn có thể gọi trực tiếp [các chức năng là các đối tượng hạng nhất trong Python giống như trong PHP> 5.3]. Nhưng những cân nhắc từ bên dưới vẫn được áp dụng. Đó là, ví dụ trên sẽ nổ tung trừ khi bạn trang trí a = A[]
method = a.method1
method[1, 2]
8 với một trong hai nhà trang trí được thảo luận dưới đây, hãy chuyển nó một ví dụ a = A[]
method = a.method1
method[1, 2]
9 là đối số đầu tiên hoặc truy cập phương thức trên một ví dụ là a = A[]
method = a.method1
method[1, 2]
9.a = A[]
method = a.method1
method[1, 2]
Bạn có ba tùy chọn để làm điều này
- Sử dụng một thể hiện là
9 để gọia = A[] method = a.method1 method[1, 2]
2 [sử dụng hai biểu mẫu có thể]class Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
- Áp dụng trình trang trí
3 choclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
2: Bạn sẽ không còn có thể tham khảoclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
5 trongclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
2 nhưng bạn sẽ được thông qua một ví dụclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
7 ở nơi đó làclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
9 trong trường hợp này.a = A[] method = a.method1 method[1, 2]
- Áp dụng trình trang trí
9 choclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
2: Bạn sẽ không còn có thể tham chiếuclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
5 hoặcclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
7 trongclass Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up def method1[self, a, b]: return a + b @staticmethod def method2[a, b]: return a + b @classmethod def method3[cls, a, b]: return cls.method2[a, b] t = Test1[] # same as doing it in another class Test1.method1[t, 1, 2] #form one of calling a method on an instance t.method1[1, 2] # form two [the common one] essentially reduces to form one Test1.method2[1, 2] #the static method can be called with just arguments t.method2[1, 2] # on an instance or the class Test1.method3[1, 2] # ditto for the class method. It will have access to the class t.method3[1, 2] # that it's called on [the subclass if called on a subclass] # but will not have access to the instance it's called on # [if it is called on an instance]
3 nhưng bạn có thể mã hóa các tài liệu tham khảo chofrom collections import Counter class FancyCounter[Counter]: def commonest[self]: [value1, count1], [value2, count2] = self.most_common[2] if count1 == count2: raise ValueError["No unique most common value"] return value1
9 vào nó, mặc dù rõ ràng, các tài liệu tham khảo này sẽ được kế thừa bằng tất cả Không gọia = A[] method = a.method1 method[1, 2]
7.from collections import Counter class FancyCounter[Counter]: def commonest[self]: [value1, count1], [value2, count2] = self.most_common[2] if count1 == count2: raise ValueError["No unique most common value"] return value1
Vài ví dụ:
class Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up
def method1[self, a, b]:
return a + b
@staticmethod
def method2[a, b]:
return a + b
@classmethod
def method3[cls, a, b]:
return cls.method2[a, b]
t = Test1[] # same as doing it in another class
Test1.method1[t, 1, 2] #form one of calling a method on an instance
t.method1[1, 2] # form two [the common one] essentially reduces to form one
Test1.method2[1, 2] #the static method can be called with just arguments
t.method2[1, 2] # on an instance or the class
Test1.method3[1, 2] # ditto for the class method. It will have access to the class
t.method3[1, 2] # that it's called on [the subclass if called on a subclass]
# but will not have access to the instance it's called on
# [if it is called on an instance]
Lưu ý rằng theo cùng một cách mà tên của biến
class Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up
def method1[self, a, b]:
return a + b
@staticmethod
def method2[a, b]:
return a + b
@classmethod
def method3[cls, a, b]:
return cls.method2[a, b]
t = Test1[] # same as doing it in another class
Test1.method1[t, 1, 2] #form one of calling a method on an instance
t.method1[1, 2] # form two [the common one] essentially reduces to form one
Test1.method2[1, 2] #the static method can be called with just arguments
t.method2[1, 2] # on an instance or the class
Test1.method3[1, 2] # ditto for the class method. It will have access to the class
t.method3[1, 2] # that it's called on [the subclass if called on a subclass]
# but will not have access to the instance it's called on
# [if it is called on an instance]
5 hoàn toàn tùy thuộc vào bạn, thì tên của biến class Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up
def method1[self, a, b]:
return a + b
@staticmethod
def method2[a, b]:
return a + b
@classmethod
def method3[cls, a, b]:
return cls.method2[a, b]
t = Test1[] # same as doing it in another class
Test1.method1[t, 1, 2] #form one of calling a method on an instance
t.method1[1, 2] # form two [the common one] essentially reduces to form one
Test1.method2[1, 2] #the static method can be called with just arguments
t.method2[1, 2] # on an instance or the class
Test1.method3[1, 2] # ditto for the class method. It will have access to the class
t.method3[1, 2] # that it's called on [the subclass if called on a subclass]
# but will not have access to the instance it's called on
# [if it is called on an instance]
7 nhưng đó là các giá trị thông thường.Bây giờ bạn đã biết cách làm điều đó, tôi sẽ nghiêm túc suy nghĩ nếu bạn muốn làm điều đó. Thông thường, các phương thức có nghĩa là được gọi là không liên kết [không có ví dụ] tốt hơn là các chức năng cấp độ mô -đun trong Python.
Đăng nhập vào tài khoản Python Barsels của bạn để lưu cài đặt screencast của bạn.
Vẫn chưa có tài khoản? Đăng ký tại đây.
Làm thế nào để thừa kế lớp hoạt động trong Python?
Tạo một lớp kế thừa từ một lớp khác
Chúng tôi có một lớp gọi là
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0, kế thừa từ một lớp khác, >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 [nằm trong mô -đun >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
2 trong thư viện tiêu chuẩn Python]:from collections import Counter
class FancyCounter[Counter]:
def commonest[self]:
[value1, count1], [value2, count2] = self.most_common[2]
if count1 == count2:
raise ValueError["No unique most common value"]
return value1
Cách chúng ta biết chúng ta đang kế thừa từ lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 bởi vì khi chúng ta xác định >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0, ngay sau tên lớp, chúng ta đặt dấu ngoặc đơn và viết >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 bên trong chúng.Để tạo một lớp kế thừa từ một lớp khác, sau tên lớp, bạn sẽ đặt dấu ngoặc đơn và sau đó liệt kê bất kỳ lớp nào mà lớp của bạn kế thừa.parentheses and then list any classes that your class inherits from.
Trong một định nghĩa chức năng, dấu ngoặc đơn sau tên hàm biểu thị các đối số mà hàm chấp nhận. Trong một định nghĩa lớp, dấu ngoặc đơn sau tên lớp thay vì đại diện cho các lớp được kế thừa.function definition, parentheses after the function name represent arguments that the function accepts. In a class definition the parentheses after the class name instead represent the classes being inherited from.
Thông thường khi thực hành kế thừa lớp học trong Python, chúng tôi thừa hưởng chỉ từ một lớp. Bạn có thể kế thừa từ nhiều lớp [được gọi là nhiều kế thừa], nhưng nó hơi hiếm. Chúng tôi sẽ chỉ thảo luận về kế thừa hạng đơn ngay bây giờ.just one class. You can inherit from multiple classes [that's called multiple inheritance], but it's a little bit rare. We'll only discuss single-class inheritance right now.
Phương pháp được kế thừa từ các lớp phụ huynh
Để sử dụng lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0 của chúng tôi, chúng tôi có thể gọi nó [giống như bất kỳ lớp nào khác]:>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
Lớp của chúng tôi sẽ chấp nhận một chuỗi khi chúng tôi gọi nó vì lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 đã triển khai phương thức >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
8 [phương thức khởi tạo].Lớp của chúng tôi cũng có phương thức
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
9 cho một biểu diễn chuỗi đẹp:>>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
Nó thậm chí còn có một loạt các chức năng khác quá. Ví dụ, nó đã ghi đè lên những gì xảy ra khi bạn sử dụng dấu ngoặc vuông để gán các cặp giá trị khóa trên các phiên bản lớp:
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
Chúng ta có thể gán các cặp giá trị khóa vì lớp cha của chúng ta,
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 tạo ra các đối tượng giống như từ điển [ánh xạ a.k.a.].Tất cả các chức năng đó được kế thừa từ lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1.inherited from the >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 class.Thêm chức năng mới trong khi kế thừa
Vì vậy, lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0 của chúng tôi thừa hưởng tất cả các chức năng mà lớp >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 của chúng tôi có nhưng chúng tôi cũng đã mở rộng nó bằng cách thêm một phương thức bổ sung, >>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
4, sẽ cung cấp cho chúng tôi mục phổ biến nhất trong lớp.Khi chúng tôi gọi phương thức
>>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
4, chúng tôi sẽ nhận được chữ >>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
6 [xảy ra ba lần trong chuỗi mà chúng tôi ban đầu cho đối tượng >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0 của chúng tôi]:>>> letters.commonest[]
'e'
Phương pháp
>>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
4 của chúng tôi dựa vào phương thức >>> letters
FancyCounter[{'e': 3, 'l': 2, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1}]
9 mà chúng tôi không xác định nhưng lớp cha mẹ của chúng tôi, >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1, đã xác định: def commonest[self]:
[value1, count1], [value2, count2] = self.most_common[2]
if count1 == count2:
raise ValueError["No unique most common value"]
return value1
Lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0 của chúng tôi có phương pháp >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
2 vì lớp cha của chúng tôi, >>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1 đã xác định nó cho chúng tôi!Ghi đè các phương pháp kế thừa
Nếu chúng tôi muốn tùy chỉnh những gì xảy ra khi chúng tôi được gán cho một cặp giá trị khóa trong lớp này, chúng tôi có thể làm điều đó bằng cách ghi đè phương thức
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4. Ví dụ: hãy làm cho nó để nếu chúng ta gán một khóa cho một giá trị âm, thay vào đó, nó gán nó cho >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
5.Trước khi chúng tôi gán
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
6 cho >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
7, chúng tôi muốn nó được đặt thành >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
5 thay vì >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
7 [đó là >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
7 ở đây vì chúng tôi chưa tùy chỉnh điều này]:>>> letters['l'] = -2
>>> letters['l']
-2
Để tùy chỉnh hành vi này, chúng tôi sẽ thực hiện một phương thức
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 chấp nhận class Test1[object]: # always inherit from object in 2.x. it's called new-style classes. look it up
def method1[self, a, b]:
return a + b
@staticmethod
def method2[a, b]:
return a + b
@classmethod
def method3[cls, a, b]:
return cls.method2[a, b]
t = Test1[] # same as doing it in another class
Test1.method1[t, 1, 2] #form one of calling a method on an instance
t.method1[1, 2] # form two [the common one] essentially reduces to form one
Test1.method2[1, 2] #the static method can be called with just arguments
t.method2[1, 2] # on an instance or the class
Test1.method3[1, 2] # ditto for the class method. It will have access to the class
t.method3[1, 2] # that it's called on [the subclass if called on a subclass]
# but will not have access to the instance it's called on
# [if it is called on an instance]
5, >>> letters.commonest[]
'e'
3 và >>> letters.commonest[]
'e'
4 bởi vì đó là những gì >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 được đưa ra bởi Python khi nó được gọi là:a = A[]
method = a.method1
method[1, 2]
0Phương pháp
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 trên về cơ bản nói: Nếu >>> letters.commonest[]
'e'
4 là âm, hãy đặt nó thành >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
5.Nếu chúng tôi ngừng viết
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 tại thời điểm này, nó sẽ không hữu ích. Trên thực tế, phương pháp >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 sẽ không làm gì cả: nó sẽ không gây ra lỗi, nhưng nó cũng không thực sự làm gì cả!Để làm điều gì đó hữu ích, chúng ta cần gọi phương thức
>>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 của lớp cha. Chúng ta có thể gọi phương thức >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 của lớp cha của chúng ta bằng cách sử dụng from collections import Counter
class FancyCounter[Counter]:
def commonest[self]:
[value1, count1], [value2, count2] = self.most_common[2]
if count1 == count2:
raise ValueError["No unique most common value"]
return value1
7.a = A[]
method = a.method1
method[1, 2]
1Chúng tôi đang gọi
def commonest[self]:
[value1, count1], [value2, count2] = self.most_common[2]
if count1 == count2:
raise ValueError["No unique most common value"]
return value1
4, sẽ gọi phương thức >>> letters['l'] = -2
>>> letters
FancyCounter[{'e': 3, 'H': 1, 'o': 1, ' ': 1, 't': 1, 'h': 1, 'r': 1, '!': 1, 'l': -2}]
4 trên lớp mẹ của chúng tôi [>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
1] với >>> letters.commonest[]
'e'
3 và >>> letters.commonest[]
'e'
4 không âm mới của chúng tôi.Dưới đây là triển khai đầy đủ phiên bản mới này của lớp
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
0 của chúng tôi:a = A[]
method = a.method1
method[1, 2]
2Để sử dụng lớp này, chúng tôi sẽ gọi nó và chuyển trong một chuỗi một lần nữa:
>>> from fancy_counter import FancyCounter
>>> letters = FancyCounter["Hello there!"]
Nhưng lần này, nếu chúng ta gán một khóa cho một giá trị âm, chúng ta sẽ thấy rằng nó sẽ được gán cho ____65 thay thế:
a = A[]
method = a.method1
method[1, 2]
4Bài tập lớp cho người mới bắt đầu nâng cao
Bạn muốn một số thực hành hơn với các lớp học trong Python?
Lặn vào đường tập thể dục bao gồm 6 bài tập lớp cho người mới bắt đầu nâng cao. Python Morsels cũng bao gồm hàng chục bài tập khác về các lớp học và Python hướng đối tượng là tốt.6 class exercises for advanced beginners. Python Morsels also includes dozens more exercises on classes and object-oriented Python as well.
✨ Hãy thử lặn vào các bài tập các lớp ✨