Hướng dẫn class string python - 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ụ: :

>>> Chain[2.5][2][2][2.5] # sum
9
>>> Chain[3][1.5][2][3] # sum
9.5

Điều tốt nhất mà tôi có thể viết là mã này:

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]

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 Khan

The need for __str__ method:

The

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method
in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
  • print[]
  • str[]

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:

If we have not defined the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method
returns a string that describes the pointer of the object by default [if the programmer does not define it].

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3, then it will call the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method returns a string that describes the pointer of the object by default [if the programmer does not define it].
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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__ method

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
1

1. Default implementation

The above code shows an example where neither

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 nor
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 are defined. Calling
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 calls the default
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method, and they all give the same output, the pointer of our object.

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
6

2. Custom __str__ method

The code above shows the output once you have defined the

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method. When
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3,
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
15, or
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
16 are called you will get your defined output. Make note that the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 output remains the same.

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
2

3. __repr__ method defined only

In the first example we saw that when

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 is not defined it automatically calls the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method. Therefore, the output of all the functions -
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3,
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
16, and
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 - are the same. Moreover, the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method does not necessarily need to return a string. In case it does not return a string, the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
15 statements will throw an error.

Shahpar Khan

The need for __str__ method:

The

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
4

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:

If we have not defined the

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
9

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3, then it will call the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method returns a string that describes the pointer of the object by default [if the programmer does not define it].
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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__ method

1. Default implementation

The above code shows an example where neither

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 nor
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 are defined. Calling
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 calls the default
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method. When
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3,
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
15, or
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
16 are called you will get your defined output. Make note that the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 output remains the same.

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
9

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
0

3. __repr__ method defined only

In the first example we saw that when

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 is not defined it automatically calls the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method. Therefore, the output of all the functions -
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3,
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
16, and
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 - are the same. Moreover, the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
7 method does not necessarily need to return a string. In case it does not return a string, the
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
15 statements will throw an error.

CONTRIBUTOR

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method in Python represents the class objects as a string – it can be used for classes. The
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
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.

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3 method is called when the following functions are invoked on the object and return a string:
class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
3

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
1

Kết quả in ra là:

class Chain[]:

    value = 0

    def __new__[self, num]:
        self.value += num
        return self

obj = Chain[2][3]
print[obj.value]
2

Nguồn và Tài liệu tiếng anh tham khảo:

  • w3school
  • python.org
  • geeksforgeeks

Tài liệu từ cafedev:

  • Full series tự học Python từ cơ bản tới nâng cao tại đây nha.
  • Ebook về python tại đây.
  • Các series tự học lập trình khác

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:

  • Group Facebook
  • Fanpage
  • Youtube
  • Instagram
  • Twitter
  • Linkedin
  • Pinterest
  • Trang chủ

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!

Bài Viết Liên Quan

Chủ Đề