Cách tạo một bản sao sâu của một đối tượng trong python

Bạn cần tạo một bản sao của một đối tượng trong chương trình Python. no kho đên mưc nao? . Nhưng bạn cũng cần biết sự khác biệt giữa sao chép nông và sâu trong Python và quyết định xem bạn cần cái nào

Trong bài viết này, bạn sẽ đọc về sự khác biệt giữa bản sao nông và sâu khi được sử dụng trên các cấu trúc dữ liệu đơn giản. Sau đó, bạn sẽ xem xét các cấu trúc phức tạp hơn, kể cả khi sao chép một đối tượng được tạo từ một lớp do chính bạn định nghĩa. Trong ví dụ này, tôi sẽ nhân bản chính mình [. ], bạn sẽ thấy một số cạm bẫy của việc sao chép đối tượng và cách phát hiện cũng như tránh chúng

Trong bài viết này, bạn sẽ tìm hiểu thêm về

  • Tạo bản sao của danh sách đơn giản và các cấu trúc dữ liệu khác
  • Tạo bản sao của danh sách phức tạp hơn
  • Sử dụng mô-đun tích hợp sẵn
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    0
  • Hiểu sự khác biệt giữa sao chép nông và sâu trong Python
  • Sử dụng
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    1 để xác định cách sao chép nông một đối tượng của lớp do người dùng định nghĩa

Vâng, cũng có

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
2, nhưng tôi sẽ dừng lại ở
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1 trong bài viết này

Vấn đề với việc sao chép các đối tượng là gì?

Đây là bản xem trước của ví dụ bạn sẽ viết ở cuối bài viết này. Bạn sẽ tạo một vài lớp đơn giản để định nghĩa một

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 và một
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5. Vâng, tôi e rằng đó lại là "người" và "xe hơi". Bạn đã thấy những ví dụ này được sử dụng rất thường xuyên trong các hướng dẫn lập trình hướng đối tượng. Nhưng nó hơi khác một chút trong trường hợp này, vì vậy hãy thông cảm cho tôi, làm ơn

Nếu bạn muốn có hướng dẫn về các lớp không sử dụng “các lớp cũ giống nhau” như mọi hướng dẫn khác, bạn có thể đọc chương về Lập trình hướng đối tượng bằng Python trong Sách viết mã Python

# household.py

class Car:
    def __init__[self, make: str, model: str]:
        self.make = make
        self.model = model
        self.mileage = 0

    def add_mileage[self, miles: float]:
        self.mileage += miles

class Person:
    def __init__[self, firstname: str]:
        self.firstname = firstname
        self.car = None

    def buy_car[self, car: Car]:
        self.car = car

    def drive[self, miles: float]:
        self.car.add_mileage[miles]

Bạn sẽ xem qua ví dụ này chi tiết hơn một chút sau. Hiện tại, Ở đây, tôi sẽ nhấn mạnh rằng mô-đun

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 có các thuộc tính
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
7,
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
8 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
9. Cái sau có thể được cập nhật bằng phương pháp
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
10

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 có các thuộc tính
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
12 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
13. Bạn có thể gán một đối tượng loại
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 cho
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 bằng cách sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
16 và bạn có thể yêu cầu người đó lái xe bằng cách sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
17, điều này sẽ tăng thêm số dặm cho ô tô

Bạn có thể sử dụng các lớp này trong một kịch bản mới

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
9

Đầu ra từ dòng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
18 là

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1

Tiếp theo, bạn sẽ sao chép Stephen [như thể một trong số tôi là chưa đủ. ]

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]

Và đây là nơi vấn đề nằm. Nhìn vào đầu ra từ mã này

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1

Xe nhái cũng là BMW Series 1, cũng hợp lý thôi. Bản sao có cùng sở thích và nhu cầu như Stephen. Nhưng, chiếc xe của bản sao bắt đầu ở 100 dặm. Mặc dù bạn vừa tạo bản sao và anh ta chưa lái xe

Dòng cuối cùng giải thích những gì đang xảy ra. Stephen và người nhân bản có cùng một chiếc xe. Không chỉ cùng kiểu dáng và kiểu dáng, mà còn giống hệt chiếc xe

Nếu bản sao lái xe ngay bây giờ, số dặm của Stephen cũng sẽ thay đổi. Đây là điều sẽ xảy ra nếu bạn thêm các dòng sau vào cuối

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5

đầu ra là

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
6

Số dặm của Stephen đã tăng thêm 68 dặm mặc dù đó là bản sao đã lái xe. Đó là bởi vì họ đang sử dụng cùng một chiếc xe. Đây không chắc là hành vi bạn muốn khi bạn tạo một bản sao của

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4

Bạn sẽ quay lại ví dụ này sau

Tạo một bản sao của cấu trúc dữ liệu đơn giản

Tôi sẽ lướt qua phần này một cách nhanh chóng khi niềm vui bắt đầu trong phần tiếp theo. Hãy sao chép một danh sách và một từ điển

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
8

Cả danh sách và từ điển đều có phương thức

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
51. Điều này làm cho cuộc sống dễ dàng sao chép chúng để tạo một đối tượng mới chứa cùng thông tin

Nếu bạn có một tuple thì sao?

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
0

Các bộ dữ liệu không có phương thức

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
51. Trong trường hợp này, bạn có thể thử sử dụng mô-đun tích hợp sẵn
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
0

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
3

Bạn đã có thể tạo một “bản sao” của một bộ, ngoại trừ việc nó hoàn toàn không phải là một bản sao. Vì các bộ dữ liệu là bất biến, nên khi bạn cố gắng sao chép bộ dữ liệu, bạn sẽ nhận được một tham chiếu mới cho cùng một bộ dữ liệu

Bạn có thể tự hỏi liệu đây cũng là trường hợp nếu bạn sử dụng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 với các loại có thể thay đổi như danh sách và từ điển

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
90

Không, trong trường hợp này,

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
55 cho kết quả giống như
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
56. Sau này bạn sẽ thấy điều gì quyết định cách thức hoạt động của
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 trên bất kỳ đối tượng nào. Nhưng trước tiên, hãy xem xét các cấu trúc dữ liệu phức tạp hơn và tìm hiểu về các bản sao nông và sâu

Tạo một bản sao của cấu trúc dữ liệu phức tạp

Xem xét một danh sách các đội, trong đó mỗi đội là một danh sách các tên. Bạn tạo một bản sao của danh sách các đội

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
91

Cho đến nay, đây là kết quả tương tự như kết quả trong phần trước. Nhưng, Martin tham gia đội của Stephen và Mary. Bạn chọn thêm danh sách này vào danh sách đã sao chép vì bạn muốn giữ nguyên danh sách

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58 ban đầu

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
92

Bạn thêm Martin vào đội đầu tiên trong

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
59. Tuy nhiên, anh ấy cũng đã được thêm vào đội đầu tiên trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58, danh sách ban đầu, mặc dù bạn không thêm bất cứ điều gì rõ ràng vào đó

Bạn có thể thấy tại sao điều này xảy ra trong câu lệnh cuối cùng mà bạn đang kiểm tra xem danh sách đầu tiên trong

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
59 có cùng đối tượng với danh sách đầu tiên trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58 hay không. Vâng, cả hai đều là cùng một đối tượng

Tạo bản sao nông và sâu trong Python

Khi bạn sao chép danh sách bằng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
63, bạn đã tạo một bản sao nông của danh sách. Hãy xem điều này có nghĩa là gì

Khi bạn tạo một danh sách, bạn đang tạo một đối tượng mới của loại danh sách có chứa một số mục. Tuy nhiên, danh sách thực sự chứa các tham chiếu đến các đối tượng khác được lưu trữ ở nơi khác. Do đó,

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
64 là một tham chiếu đến một đối tượng khác, danh sách.
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
65. Nhìn lại dòng bạn đã sử dụng để tạo danh sách
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58 ban đầu

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
93

Dòng này tạo ra ba danh sách

  • Danh sách
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    65
  • Danh sách
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    68
  • Danh sách có tên
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    58 có tham chiếu đến hai danh sách khác

Bạn có thể hình dung điều này bằng sơ đồ bên dưới

Khi bạn sử dụng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
63 hoặc
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
81, bạn đang tạo một danh sách bên ngoài mới. Tuy nhiên, bạn không sao chép các danh sách bên trong. Thay vào đó, bạn sử dụng cùng một danh sách
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
65 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
68 mà bạn đã có. Đây là một đại diện của những gì nó trông giống như

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
64 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
85 là hai tham chiếu trỏ đến cùng một danh sách. Bạn có hai cách đề cập đến cùng một đối tượng

Vì vậy, khi bạn thêm Martin vào

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
85, bạn đang thêm tên của Martin vào danh sách hiện có duy nhất có tên của các thành viên trong nhóm của Stephen

Đôi khi, đây không phải là điều bạn muốn. Thay vào đó, bạn muốn tạo một bản sao của tất cả các mục bên trong đối tượng

Sao chép sâu

Trong phần này, bạn sẽ đọc về cách tạo một bản sao sâu của một đối tượng. Nhưng trước tiên, hãy tạo lại ví dụ trên bằng cách sử dụng các hàm trong mô-đun tích hợp sẵn

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
0

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 tạo một bản sao nông, vì vậy bạn sẽ nhận được kết quả đầu ra giống như trong phần trên

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
94

Do đó, đối với danh sách,

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
81 giống như
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
63

Tiếp theo, bạn có thể thử sử dụng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
01 để thay thế

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
95

Khi bạn thêm

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
02 vào
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
03, đây là bản sao sâu mà bạn đã tạo từ danh sách gốc, mục mới không xuất hiện khi bạn hiển thị
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58. Và không giống như trường hợp của bản sao nông trước đó,
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
05 không còn là đối tượng giống như
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
64

Khi bạn tạo bản sao sâu, bạn đang sao chép danh sách bên ngoài, nhưng bạn cũng đang tạo bản sao của danh sách bên trong. Do đó, các tham chiếu trong

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
58 và những tham chiếu trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
03 trỏ đến các đối tượng khác nhau. Hai bản sao được tạo bởi
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
09 hoàn toàn tách biệt với nhau. Đây là cách đại diện này trông bây giờ

Bạn có thể đọc thêm về bản sao nông và sâu trong Python trong tài liệu chính thức

Sao chép các đối tượng của các lớp do bạn tự định nghĩa

Đã đến lúc tạo các lớp của riêng bạn và khám phá điều gì sẽ xảy ra khi bạn tạo các bản sao của chúng. Bạn đã bắt gặp các định nghĩa lớp

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 ở đầu bài viết này. Hãy giới thiệu các lớp này đúng cách. Bạn có thể định nghĩa chúng trong tập lệnh có tên là
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
32

# household.py

class Car:
    def __init__[self, make: str, model: str]:
        self.make = make
        self.model = model
        self.mileage = 0

    def add_mileage[self, miles: float]:
        self.mileage += miles

class Person:
    def __init__[self, firstname: str]:
        self.firstname = firstname
        self.car = None

    def buy_car[self, car: Car]:
        self.car = car

    def drive[self, miles: float]:
        self.car.add_mileage[miles]

Bạn có thể khởi tạo

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 bằng một
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
7 và một
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
8, cả hai đều là chuỗi. Tôi đang sử dụng gợi ý kiểu trong ví dụ này để theo dõi xem các kiểu đối số là gì. Một chiếc ô tô mới bắt đầu với 0 dặm [hoặc km, nếu bạn muốn]

Và như tên của nó, phương pháp

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
10 được sử dụng để thêm dặm bất cứ khi nào người đó lái xe

Một

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 được khởi tạo với tên là một chuỗi. Phương thức
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
16 cho phép bạn liên kết một thể hiện của lớp
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 với một thể hiện của
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4. Đối tượng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 được tham chiếu bằng thuộc tính
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
903

Bất cứ khi nào người đó đi trên một chuyến đi, bạn có thể gọi phương thức

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
17 để ghi lại số dặm bổ sung vào ô tô của người đó

Trong tập lệnh mới có tên là

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19, bạn có thể kiểm tra các lớp này

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
9

Đây là mã giống như bạn đã thấy trước đó. Bạn tạo một thể hiện của

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 và gọi phương thức
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
16 cho thể hiện đó. Stephen [Tôi vẫn đang nói về bản thân mình ở ngôi thứ ba. ] lái xe 100 dặm. Bạn ghi nhật ký này bằng cách gọi phương thức
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
17. Điều này cập nhật thuộc tính
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
9 của phiên bản
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 được tham chiếu trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
911. Mã này cho đầu ra sau

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1

Sao chép một đối tượng. Trường hợp mặc định

Stephen rất bận rộn trong những ngày này. Anh ấy quyết định nhân bản chính mình để có thể làm được nhiều việc hơn. Chúng ta hãy cố gắng này. Bạn có thể sao chép phiên bản

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
912 trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19 bằng cách sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 tích hợp

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]

Các đầu ra từ tập lệnh này, mà bạn đã thấy trước đó, cho thấy vấn đề với loại bản sao này

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1

Đây là một bản sao nông. Do đó, mặc dù

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
912 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
916 là các thể hiện khác nhau của lớp
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4, nhưng cả hai đều chia sẻ cùng một thể hiện của
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5. Stephen đã thành công trong việc nhân bản chính mình, nhưng anh ấy phải đi chung xe với người nhân bản của mình. Điều đó không tốt, vì Stephen và bản sao không thể hoạt động hiệu quả nếu họ không thể đi đến những nơi khác nhau

Nếu bản sao lái xe, anh ta sẽ sử dụng cùng một chiếc xe mà Stephen sử dụng. Do đó, số dặm bổ sung cũng sẽ hiển thị cho Stephen

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5

Điều này cho thấy số dặm của Stephen đã tăng lên 168 dặm

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
6

Sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
01

Điều gì sẽ xảy ra nếu bạn cố gắng tạo một bản sao sâu thay vì một bản sao nông? . Bạn có thể cập nhật

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19 để sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
01 thay vì
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
13

Khi bạn chạy tập lệnh này, bây giờ bạn sẽ nhận được đầu ra sau

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
14

Số dặm của Stephen vẫn là 100 dặm. Không có lý do tại sao điều này nên khác khi Stephen lái xe 100 dặm

Chiếc xe của bản sao là BMW Series 1, giống với kiểu dáng và kiểu dáng xe của Stephen. Đây là những gì bạn muốn vì bản sao của Stephen có cùng sở thích về ô tô với Stephen

Hãy bỏ qua dòng cuối cùng của đầu ra. Xe của Stephen không còn giống hệt xe của người nhân bản nữa. Điều này khác với kết quả bạn nhận được với bản sao nông ở trên. Xe của người nhân bản là một ví dụ khác của

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5. Vì vậy, có hai chiếc xe bây giờ;

Tuy nhiên, xe của người nhái đã đi được 100 dặm trên đồng hồ đo quãng đường mặc dù người nhái vẫn chưa lái. Khi bạn tạo một bản sao sâu của

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
912, chương trình sẽ tạo một phiên bản mới của
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5. Tuy nhiên, tất cả các thuộc tính ban đầu của xe cũng được sao chép. Điều này có nghĩa là xe của bản sao bắt đầu với bất kỳ quãng đường nào mà xe của Stephen có khi bạn tạo bản sao sâu

Kể từ bây giờ, hai chiếc xe là riêng biệt, vì vậy khi người nhân bản lái xe, số dặm bổ sung sẽ không hiển thị trong xe của Stephen

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
15

Kết quả cho thấy số dặm của Stephen vẫn là 100 dặm, nhưng số dặm của bản sao hiện là 168 dặm mặc dù chuyến đi duy nhất và duy nhất của anh dài 68 dặm

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
16

Trong phần cuối của bài viết này, bạn sẽ sửa lỗi này để tùy chỉnh cách sao chép một phiên bản của

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4

Xác định Phương pháp Dunder
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
927

Bạn có thể ghi đè hành vi mặc định cho

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
01 cho bất kỳ lớp nào bạn xác định. Trong bài viết này, tôi sẽ chỉ tập trung vào việc định nghĩa phương thức dunder
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1, phương thức này xác định điều gì sẽ xảy ra khi bạn gọi
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 cho đối tượng của mình. Ngoài ra còn có một phương thức
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
2 dunder, nhằm mục đích tạo các bản sao sâu, tương tự nhưng cung cấp thêm một chút chức năng để xử lý các đối tượng phức tạp

Bạn có thể quay lại

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
32 nơi bạn định nghĩa lớp
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 và thêm
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1 vào lớp

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
17

Phương thức

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1 dunder tạo một phiên bản
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 mới bằng cách sử dụng cùng tên của phiên bản bạn đang sao chép. Nó cũng tạo một phiên bản
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 mới bằng cách sử dụng kiểu dáng và kiểu dáng của chiếc ô tô mà bạn đang sao chép. Bạn chuyển đối tượng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 mới này làm đối số trong
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
940 và sau đó trả lại đối tượng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4 mới

Bạn có thể quay lại

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19, đảm bảo rằng bạn sử dụng
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 để tạo bản sao của
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
912. Điều này có nghĩa là
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
945 được sử dụng khi tạo bản sao

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]

Bây giờ, đầu ra là

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
19

Bản sao vẫn có một phiên bản khác là

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
5 nhưng bây giờ, quãng đường của chiếc xe bắt đầu từ 0, như bạn mong đợi. Bạn đã tạo một phiên bản tùy chỉnh của bản sao nông bằng cách xác định
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1 cho lớp. Trong trường hợp này, bạn đã quyết định rằng khi bạn sao chép một
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
4, phiên bản mới có ô tô riêng bắt đầu bằng 0 dặm

Trong các lớp phức tạp hơn, bạn có thể muốn xác định cả

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
1 và
# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
2 nếu bạn muốn phân biệt giữa bản sao nông và sâu trong chương trình Python của mình

Từ cuối cùng

Dưới đây là tóm tắt những điểm chính mà bạn đã trình bày trong bài viết này

  • Bạn đã tạo các bản sao của danh sách đơn giản và các cấu trúc dữ liệu khác
  • Bạn đã tạo bản sao của các danh sách phức tạp hơn
  • Bạn đã sử dụng mô-đun tích hợp sẵn
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    0
  • Bạn đã tìm hiểu về sự khác biệt giữa sao chép nông và sâu trong Python
  • Bạn đã sử dụng
    # cloning_stephen.py
    
    import copy
    
    from household import Car, Person
    
    # Create a person who buys a car
    stephen = Person["Stephen"]
    stephen.buy_car[
        Car["BMW", "Series 1"]
    ]
    
    # Log how many miles driven
    stephen.drive[100]
    
    print[f"Stephen's mileage is {stephen.car.mileage} miles"]
    
    # Let's copy the Person instance
    clone = copy.copy[stephen]
    
    print[
        f"The clone's car is a {clone.car.make} {clone.car.model}"
    ]
    
    print[f"The clone's mileage is {clone.car.mileage} miles"]
    
    # Let's check whether the two cars are exactly the same car
    print[
        f"Stephen's car is clone's car: {stephen.car is clone.car}"
    ]
    1 để xác định cách sao chép nông một đối tượng của lớp do người dùng định nghĩa

Bây giờ bạn đã sẵn sàng để sao chép bất kỳ đối tượng nào một cách an toàn, biết những điều cần chú ý nếu đối tượng tham chiếu đến các đối tượng khác

ruột thừa. Bạn không thể sao chép một đối tượng bất biến

Bạn có nhớ khi bạn sử dụng

# cloning_stephen.py

import copy

from household import Car, Person

# Create a person who buys a car
stephen = Person["Stephen"]
stephen.buy_car[
    Car["BMW", "Series 1"]
]

# Log how many miles driven
stephen.drive[100]

print[f"Stephen's mileage is {stephen.car.mileage} miles"]

# Let's copy the Person instance
clone = copy.copy[stephen]

print[
    f"The clone's car is a {clone.car.make} {clone.car.model}"
]

print[f"The clone's mileage is {clone.car.mileage} miles"]

# Let's check whether the two cars are exactly the same car
print[
    f"Stephen's car is clone's car: {stephen.car is clone.car}"
]
54 trên một tuple trước đó trong bài viết không?

Làm cách nào tôi có thể tạo một bản sao của một đối tượng trong Python?

Sao chép các đối tượng Python tùy ý . copy. sao chép[] và sao chép. Các hàm deepcopy[] có thể được sử dụng để sao chép bất kỳ đối tượng nào.

Deepcopy[] trong Python là gì?

Một bản sao nông xây dựng một đối tượng phức hợp mới và sau đó [trong phạm vi có thể] chèn vào đó các tham chiếu tới các đối tượng được tìm thấy trong bản gốc. Một bản sao sâu xây dựng một đối tượng phức hợp mới và sau đó, theo cách đệ quy, chèn các bản sao của các đối tượng được tìm thấy trong bản gốc vào đó.

Copy[] có tạo bản sao sâu không?

bản sao[] trả về một bản sao nông của danh sách và deepcopy[] trả về một bản sao sâu của danh sách . Như bạn có thể thấy rằng cả hai đều có cùng giá trị nhưng có ID khác nhau.

Bản sao sâu của một đối tượng là gì?

Sao chép sâu là một quá trình trong đó quá trình sao chép diễn ra theo cách đệ quy. Nó có nghĩa là đầu tiên xây dựng một đối tượng bộ sưu tập mới và sau đó điền đệ quy nó với các bản sao của các đối tượng con được tìm thấy trong bản gốc. Trong trường hợp sao chép sâu, một bản sao của đối tượng được sao chép vào đối tượng khác

Chủ Đề