Hướng dẫn return string class python - trả về chuỗi lớp python
Xin chào Trong một cuộc thi lập trình địa phương gần đây, có một vấn đề trong đó bạn phải xác định một lớp có các tham số giống như chuỗi và trả về tổng của chúng, ví dụ: : Show
Điều tốt nhất mà tôi có thể viết là mã này:
Nhưng loại trả lại là một lớp không phải là INT, hơn nữa tôi đang sử dụng một thuộc tính tĩnh rõ ràng là sai. Sẽ đánh giá cao bất kỳ sự giúp đỡ. P.S. : Xin lưu ý rằng cuộc thi đó kết thúc vào ngày 7 tháng 1 năm 2022 vì vậy tôi không nghĩ có bất kỳ vấn đề nào với việc đăng câu hỏi này. Shahpar KhanThe need for __str__ method:The 3 method in Python represents the class objects as a string – it can be used for classes. The 3 method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked. 3 method in Python represents the class objects as a string – it can be used for classes. The 3 method
should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.The 3 method is called when the following functions are invoked on the object and return a string:
If we have not defined the 3, then it will call the 7 method. The 7 method returns a string that describes the pointer of the object by default (if the programmer does not define it). 7 method returns a string that describes the pointer of
the object by default (if the programmer does not define it).How to call __str__ method1. Default implementationclass MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject) The above code shows an example where neither 3 nor 7 are defined. Calling 3 calls the default 7 method, and they all give the same output, the pointer of our object.2. Custom __str__ methodclass MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString def __str__ (self): return 'MyClass(x=' + str(self.x) + ' ,y=' + self.y + ')' myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject) print(str(myObject)) print(myObject.__repr__()) The code above shows the output once you have defined the 3 method. When 3, class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)5, or class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)6 are called you will get your defined output. Make note that the 7 output remains the same.3. __repr__ method defined onlyclass MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString def __repr__ (self): return 'MyClass(x=' + str(self.x) + ' ,y=' + self.y + ')' myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject) print(str(myObject)) print(myObject.__repr__()) In the first example we saw that when 3 is not defined it automatically calls the 7 method. Therefore, the output of all the functions - 3, class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)6, and 7 - are the same. Moreover, the 7 method does not necessarily need to return a string. In case it does not return a string, the class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)5 statements will throw an error. CONTRIBUTOR Shahpar Khan The need for __str__ method: The 3 method in Python represents the class objects as a string – it can be used for classes. The 3 method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.The 3 method is called when the following functions are invoked on the object and return a string:
Kết quả in ra là:
If we have not defined the 3, then it will call the 7 method. The 7 method returns a string that describes the pointer of the object by default (if the programmer does not define it).
Kết quả in ra là:
How to call __str__ method 1. Default implementation The above code shows an example where neither 3 nor 7 are defined. Calling 3 calls the default 7 method, and they all give the same output, the pointer of our object.2. Custom __str__ method The code above shows the output once you have defined the 3 method. When 3, class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)5, or class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)6 are called you will get your defined output. Make note that the 7 output remains the same.3. __repr__ method defined only
Kết quả in ra là: 0In the first example we saw that when 3 is not defined it automatically calls the 7 method. Therefore, the output of all the functions - 3, class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)6, and 7 - are the same. Moreover, the 7 method does not necessarily need to return a string. In case it does not return a string, the class MyClass: x = 0 y = "" def __init__(self, anyNumber, anyString): self.x = anyNumber self.y = anyString myObject = MyClass(12345, "Hello") print(myObject.__str__()) print(myObject.__repr__()) print(myObject)5 statements will throw an error. CONTRIBUTOR Copyright ©2022 Educative, Inc. All rights reserved Cả hai hàm str() và repr() đều được sử dụng để lấy về dạng thức kiểu string của một đối tượng Dưới đây là đoạn chương trình Python mô tả cách viết hàm__repr__ và hàm __str__ cho một class do người dùng tự định nghĩa. 1Kết quả in ra là: 2Nguồn và Tài liệu tiếng anh tham khảo:
Tài liệu từ cafedev:
Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:
Chào thân ái và quyết thắng! Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you! |