Python mặc định __new__

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

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 để tạo đối tượng và sau đó gọi phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
5 để khởi tạo các thuộc tính của đối tượng

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 là một phương thức tĩnh của lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
7. Nó có chữ ký sau

object.__new__(class, *args, **kwargs)

Code language: Python (python)

Đối số đầu tiên của phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
8 là

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
9 của đối tượng mới mà bạn muốn tạo

Các tham số

person = Person('John')

Code language: Python (python)
0 và

person = Person('John')

Code language: Python (python)
1 phải khớp với các tham số của

person = Person('John')

Code language: Python (python)
2 của lớp. Tuy nhiên, phương pháp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 không sử dụng chúng

Phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 sẽ trả về một đối tượng mới của lớp. Nhưng nó không phải

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

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
7. Điều đó có nghĩa là bạn có thể ghi đè phương thức tĩnh của

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
8 và làm điều gì đó trước và sau khi tạo một thể hiện mới của lớp

Để tạo đối tượng của một lớp, bạn gọi phương thức

person = Person('John')

Code language: Python (python)
7

Về mặt kỹ thuật, bạn có thể gọi phương thức

person = Person('John')

Code language: Python (python)
8 để tạo đối tượng theo cách thủ công. Tuy nhiên, bạn cần tự gọi

person = Person('John')

Code language: Python (python)
2 theo cách thủ công sau khi. Python sẽ không tự động gọi phương thức

person = Person('John')

Code language: Python (python)
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ức

person = Person('John')

Code language: Python (python)
8

Ví dụ về phương thức __new__ trong Python

Sau đây định nghĩa lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
92 với thuộc tính

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
93 và tạo một thể hiện mới của lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
92

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)

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

person = Person('John')

Code language: Python (python)

Python sẽ gọi các phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 và

person = Person('John')

Code language: Python (python)
2. Nó tương đương với các cuộc gọi phương thức sau

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
9

Phần sau hiển thị nội dung của đối tượng

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
97 của đối tượng

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
98 sau khi gọi phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 và

person = Person('John')

Code language: Python (python)
2

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
1

đầu ra

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
2

Như bạn có thể thấy rõ từ đầu ra, sau khi gọi phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4,

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
12 trống. Và sau khi gọi phương thức

person = Person('John')

Code language: Python (python)
2,

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
12 chứa thuộc tính

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
93 với giá trị ‘

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
16

Phần sau đây minh họa trình tự mà Python gọi phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
8 và

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
18 khi bạn tạo một đối tượng mới bằng cách gọi lớp

object.__new__(class, *args, **kwargs)

Code language: Python (python)
1

đầu ra

object.__new__(class, *args, **kwargs)

Code language: Python (python)
2

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

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
19 kế thừa từ kiểu int có sẵn

object.__new__(class, *args, **kwargs)

Code language: Python (python)
4

Trong ví dụ này, phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 của lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
19 chấp nhận một số nguyên và trả về số bình phương.

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
22 là một thể hiện của lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
19 và cũng là một thể hiện của loại dựng sẵn

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
24

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
0

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

person = Person('John')

Code language: Python (python)
2 vì phương thức

person = Person('John')

Code language: Python (python)
2 của

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
24 tích hợp sẵn không có đối số. Đoạn mã sau sẽ dẫn đến lỗi

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
0

Lỗi

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
1

Trong thực tế, bạn sử dụng phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4 khi muốn tinh chỉnh đối tượng tại thời điểm khởi tạo

Ví dụ: phần sau định nghĩa lớp

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
92 và sử dụng phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
8 để đưa thuộc tính

object.__new__(class, *args, **kwargs)

Code language: Python (python)
11 vào đối tượng của Người

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
2

đầu ra

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
3

Thông thường, khi bạn ghi đè phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4, bạn không cần xác định phương thức

person = Person('John')

Code language: Python (python)
2 vì mọi thứ bạn có thể làm trong phương thức

person = Person('John')

Code language: Python (python)
2, bạn có thể làm điều đó trong phương thức

class Person: def __init__(self, name): self.name = name person = Person('John')

Code language: Python (python)
4

__ mới __ trong Python là gì?

Python __new__() là phương thức khởi tạo kiểm soát việc tạo phiên bản mới . Nó được gọi đầu tiên và nó trả về một thể hiện của lớp mới. Python __init__() là phương thức khởi tạo để thiết lập các thuộc tính (i. e. , trạng thái) của phiên bản mới được tạo. Nó được gọi sau khi tạo và không trả về gì cả, tôi. e. , Không có.

Tại sao nên sử dụng __ mới __ trong Python?

Theo tài liệu chính thức của Python, __new__ được sử dụng khi bạn cần kiểm soát việc tạo phiên bản mới trong khi __init__ được sử dụng để kiểm soát .

Sự khác biệt giữa __ khởi tạo __ và __ mới __ là gì?

__new__ trả về một thể hiện của lớp. __init__ nhận các thể hiện của lớp được trả về bởi __new__ . Sử dụng __init__ để khởi tạo giá trị.

Có từ khóa mới trong Python không?

Python không có từ khóa mới .