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 Show
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ề
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ảnTô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ạpXem 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 PythonKhi 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
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âuTrong 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 địnhStephen 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 |