Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về phương thức __new__ của Python và hiểu cách Python sử dụng nó để tạo một đối tượng mới
Giới thiệu về phương thức __new__ của Python
Khi bạn tạo một thể hiện của một lớp, trước tiên Python gọi phương thức
4 để tạo đối tượng và sau đó gọi phương thứcCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
5 để khởi tạo các thuộc tính của đối tượngCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
4 là một phương thức tĩnh của lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
7. Nó có chữ ký sauCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Code language: Python [python]
object.__new__[class, *args, **kwargs]
Đối số đầu tiên của phương thức
8 làCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
9 của đối tượng mới mà bạn muốn tạoCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Các tham số
0 vàCode language: Python [python]
person = Person['John']
1 phải khớp với các tham số củaCode language: Python [python]
person = Person['John']
2 của lớp. Tuy nhiên, phương phápCode language: Python [python]
person = Person['John']
4 không sử dụng chúngCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Phương thức
4 sẽ trả về một đối tượng mới của lớp. Nhưng nó không phảiCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Khi bạn định nghĩa một lớp mới, lớp đó sẽ kế thừa hoàn toàn từ lớp
7. Điều đó có nghĩa là bạn có thể ghi đè phương thức tĩnh củaCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
8 và làm điều gì đó trước và sau khi tạo một thể hiện mới của lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Để tạo đối tượng của một lớp, bạn gọi phương thức
7Code language: Python [python]
person = Person['John']
Về mặt kỹ thuật, bạn có thể gọi phương thức
8 để tạo đối tượng theo cách thủ công. Tuy nhiên, bạn cần tự gọiCode language: Python [python]
person = Person['John']
2 theo cách thủ công sau khi. Python sẽ không tự động gọi phương thứcCode language: Python [python]
person = Person['John']
2 nếu bạn rõ ràng tạo một đối tượng mới bằng cách sử dụng phương thứcCode language: Python [python]
person = Person['John']
8Code language: Python [python]
person = Person['John']
Ví dụ về phương thức __new__ trong Python
Sau đây định nghĩa lớp
92 với thuộc tínhCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
93 và tạo một thể hiện mới của lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
92Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Trong Python, một lớp có thể gọi được. Khi bạn gọi lớp để tạo một đối tượng mới
Code language: Python [python]
person = Person['John']
Python sẽ gọi các phương thức
4 vàCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
2. Nó tương đương với các cuộc gọi phương thức sauCode language: Python [python]
person = Person['John']
9Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Phần sau hiển thị nội dung của đối tượng
97 của đối tượngCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
98 sau khi gọi phương thứcCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
4 vàCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
2Code language: Python [python]
person = Person['John']
1Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
đầu ra
2Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Như bạn có thể thấy rõ từ đầu ra, sau khi gọi phương thức
4,Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
12 trống. Và sau khi gọi phương thứcCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
2,Code language: Python [python]
person = Person['John']
12 chứa thuộc tínhCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
93 với giá trị ‘Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
16Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Phần sau đây minh họa trình tự mà Python gọi phương thức
8 vàCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
18 khi bạn tạo một đối tượng mới bằng cách gọi lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
1Code language: Python [python]
object.__new__[class, *args, **kwargs]
đầu ra
2Code language: Python [python]
object.__new__[class, *args, **kwargs]
Trong ví dụ này, Python gọi phương thức __new__ và phương thức __init__ sau đó
Khi sử dụng phương thức __new__
Ví dụ sau định nghĩa lớp
19 kế thừa từ kiểu int có sẵnCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
4Code language: Python [python]
object.__new__[class, *args, **kwargs]
Trong ví dụ này, phương thức
4 của lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
19 chấp nhận một số nguyên và trả về số bình phương.Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
22 là một thể hiện của lớpCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
19 và cũng là một thể hiện của loại dựng sẵnCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
24
class Person: def __init__[self, name]: self.name = name person = Person['John']
Code language: Python [python]
0Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Lưu ý rằng bạn không thể thực hiện việc này bằng cách sử dụng phương thức
2 vì phương thứcCode language: Python [python]
person = Person['John']
2 củaCode language: Python [python]
person = Person['John']
24 tích hợp sẵn không có đối số. Đoạn mã sau sẽ dẫn đến lỗiCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
0Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Lỗi
1Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Trong thực tế, bạn sử dụng phương thức
4 khi muốn tinh chỉnh đối tượng tại thời điểm khởi tạoCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Ví dụ: phần sau định nghĩa lớp
92 và sử dụng phương thứcCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
8 để đưa thuộc tínhCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
11 vào đối tượng của NgườiCode language: Python [python]
object.__new__[class, *args, **kwargs]
2Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
đầu ra
3Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
Thông thường, khi bạn ghi đè phương thức
4, bạn không cần xác định phương thứcCode language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']
2 vì mọi thứ bạn có thể làm trong phương thứcCode language: Python [python]
person = Person['John']
2, bạn có thể làm điều đó trong phương thứcCode language: Python [python]
person = Person['John']
4Code language: Python [python]
class Person: def __init__[self, name]: self.name = name person = Person['John']