Bạn đã viết một phiên bản mới của phương thức
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 cho class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
7. Trong lớp đó, phiên bản mới của phương thức class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 sẽ thay thế phiên bản bạn có cho lớp class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
9. Đó là một cách để làm điều đó, mà các hướng dẫn mô tảTuy nhiên, vì bạn muốn, thay vào đó, gọi phiên bản
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
9 của class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 trong phương thức class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 mới để tránh phải viết lại toàn bộ phương thức từ đầu, sau đó thêm một câu lệnh khác để xử lý tham số mới và biến đối tượng, bạn có thể thực hiện điều này class ElectricCar[Car]:
def __init__[self, model, color, mpg, battery_type]:
super[ElectricCar, self].__init__[model, color, mpg]
self.battery_type = battery_type
Gần đây tôi đã hỏi tôi phương thức
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 dunder đã làm gì trong Python. Và tôi đã nói, "nó được sử dụng để khởi tạo các biến bên trong một lớp". Và ngay sau đó, câu hỏi tiếp theo là, “vậy thì class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 dùng để làm gì?”. Và tôi hoàn toàn trống rỗng và không thể trả lời rằngTôi không thể trả lời câu hỏi đó vì không có nhiều hướng dẫn nói về
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2. Tôi không muốn xảy ra điều này với bạn. Và đó là lý do tại sao tôi nghĩ ra bài viết blog này cho bạnĐiểm tương đồng
Hãy bắt đầu với những điểm tương đồng
- Cả hai đều được gọi/gọi trong quá trình tạo thể hiện
sự khác biệt
Hãy bắt đầu với sự khác biệt
newinit1Được gọi trước initĐược gọi sau new2Chấp nhận một loại làm đối số đầu tiênChấp nhận một thể hiện làm đối số đầu tiên3Được cho là trả về một thể hiện của loại đã nhậnKhông được phép trả về bất kỳ thứ gì4Được sử dụng để kiểm soát việc tạo cá thểĐược sử dụng để khởi tạo các biến thể hiệnNói về điểm đầu tiên.
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 được gọi khi phiên bản được tạo lần đầu tiên. Điều này xảy ra trước khi khởi tạo lớpNhân tiện, bạn có lưu ý rằng đối số đầu tiên của
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 luôn là class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 không? . class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6 là những gì class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 trả vềĐến điểm thứ ba,
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 được cho là trả về một thể hiện của lớp. Lưu ý rằng nếu class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 không trả về bất cứ thứ gì, thì class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 sẽ không được gọiMà một trong số họ là một nhà xây dựng?
Nếu bạn đến từ một ngôn ngữ khác, bạn có thể ngạc nhiên rằng có hai điều giống nhau đang làm cùng một loại công việc. Hầu hết các ngôn ngữ mà bạn có thể đã từng làm việc sẽ có một thứ gọi là hàm tạo
Trong Python, khái niệm đó được chia thành hàm tạo và trình khởi tạo. Và bạn có thể đoán ra,
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 là hàm tạo và class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 là hàm khởi tạoXin lưu ý rằng
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 là ẩn. Có nghĩa là nếu bạn không thực sự cần sửa đổi việc tạo một thể hiện của lớp, thì bạn không cần phải có phương thức class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
55Một điều nữa tôi muốn thêm vào là… các biến thể hiện là cục bộ của một thể hiện. Vì vậy, bất cứ điều gì bạn đang làm trong init chỉ là cục bộ của phiên bản đó. Nhưng bất cứ điều gì bạn đang làm mới sẽ ảnh hưởng đến mọi thứ được tạo cho loại đó
Luồng thực thi với một ví dụ
Tôi sẽ thêm một số mã để làm cho điều này hấp dẫn hơn. Hãy xem xét ví dụ này
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
Đây là ví dụ đơn giản nhất về cả
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 và class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 đang hoạt động. Nếu bạn lưu đoạn mã trên vào một tệp và chạy nó, bạn sẽ thấy như thế nàyclass Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
5Như bạn có thể thấy, phương thức mới được gọi đầu tiên và sau đó việc thực thi được chuyển sang phương thức init
Trường hợp sử dụng
Trường hợp sử dụng cho class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
Một trong những trường hợp sử dụng tốt nhất mà tôi có thể lấy làm ví dụ là khi tạo một Singleton. Như chúng ta đã biết, Singleton đảm bảo một lớp chỉ có một thể hiện và cung cấp một điểm truy cập toàn cục cho nó
Một số nơi tôi đã thấy singleton đang hoạt động là trong lập trình trò chơi nơi chỉ có một phiên bản của trình phát. Một nơi khác, nếu bạn đã sử dụng các thư viện frontend như Vuex [hoặc Redux] thì chỉ có một phiên bản toàn cầu của cửa hàng. Không quan trọng bạn tạo bao nhiêu phiên bản, cuối cùng bạn sẽ chỉ có một
Hãy xem cách đạt được hành vi tương tự trong Python
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2____23đầu ra
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
4Như bạn có thể thấy, việc tạo… chỉ được in một lần. cả hai đều trỏ đến cùng một vị trí bộ nhớ. Trong trường hợp của tôi, đó là
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
59Bạn có thể đoán rằng chúng ta không thể làm điều tương tự với
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1? . Đó là bởi vì class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 không trả lại bất cứ thứ gì. Chúng ta sẽ xem trong phần tiếp theo class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 phù hợp với điều gìNhưng trước tiên, tôi muốn cho bạn thấy một trường hợp sử dụng khác của new
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
9class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
0đầu ra
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1Tôi không phải là nhà động vật học. Nhưng bạn hiểu ý tôi ở đây. Bạn có thể sử dụng
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2 để tạo một thể hiện có điều kiện từ một lớpTrường hợp sử dụng cho class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
Như chúng ta đã thấy trước đây.
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
25 ở đó để khởi tạo một biến thể hiện. Các biến đối tượng này sau này có thể được sử dụng trong các phương thức khác nhau của đối tượngTôi đã sử dụng rộng rãi
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 khi tôi từng làm việc với Qt framework. Qt là một khuôn khổ để phát triển giao diện người dùng dựa trên máy tính để bàn. Khi khởi tạo các đối tượng giao diện người dùng, bạn có thể đặt độ rộng hoặc độ dài của cửa sổ. Bạn cũng có thể đọc các tùy chọn từ một tệp và áp dụng tùy chọn đó trong giai đoạn khởi tạo ứng dụng. Đặt tiêu đề cửa sổ có thể là một ví dụ khácỞ đây tôi sẽ chứng minh một ví dụ như vậy
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
6class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
0Ví dụ trên không phải là một ví dụ hoàn chỉnh, nhưng khi được thiết lập chính xác, nó sẽ hiển thị một cửa sổ tương tự như thế này
Chìa khóa rút ra
- Trong hầu hết các trường hợp, bạn không cần
2 được gọi trướcclass Demo: def __new__[cls, *args]: print["__new__ called"] return object.__new__[cls] def __init__[self]: print["__init__ called"] d = Demo[]
1class Demo: def __new__[cls, *args]: print["__new__ called"] return object.__new__[cls] def __init__[self]: print["__init__ called"] d = Demo[]
2 trả về một thể hiện của lớpclass Demo: def __new__[cls, *args]: print["__new__ called"] return object.__new__[cls] def __init__[self]: print["__init__ called"] d = Demo[]
1 nhận các thể hiện của lớp được trả về bởiclass Demo: def __new__[cls, *args]: print["__new__ called"] return object.__new__[cls] def __init__[self]: print["__init__ called"] d = Demo[]
2class Demo: def __new__[cls, *args]: print["__new__ called"] return object.__new__[cls] def __init__[self]: print["__init__ called"] d = Demo[]
- Sử dụng _______ 11 để khởi tạo giá trị
Khái niệm tương tự có thể được sử dụng để trả lời câu hỏi trừu tượng hóa và đóng gói
Phần kết luận
Đó là tất cả những gì tôi biết về
class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
1 so với class Demo:
def __new__[cls, *args]:
print["__new__ called"]
return object.__new__[cls]
def __init__[self]:
print["__init__ called"]
d = Demo[]
2. Nếu bạn có một cái gì đó trong tâm trí của bạn mà tôi bỏ lỡ. Làm ơn cho tôi biếtTôi cũng sẽ liệt kê một số tài liệu tham khảo mà tôi muốn viết bài đăng trên blog này để bạn có thể thực sự là nguồn thông tin thực sự.