Hướng dẫn what does __ repr __ do python? - __ repr __ python làm gì?

Hướng dẫn what does __ repr __ do python? - __ repr __ python làm gì?
Nouman Abbasi

Trong Python,

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 là một phương pháp đặc biệt được sử dụng để thể hiện các đối tượng lớp lớp dưới dạng chuỗi.

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 được gọi bởi chức năng tích hợp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
3. Bạn có thể xác định biểu diễn chuỗi của riêng bạn của các đối tượng lớp bằng phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1.

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1
is a special method used to represent a class’s objects as a string.

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 is called by the

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
3 built-in function. You can define your own string representation of your class objects using the

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 method.

Các phương pháp đặc biệt là một tập hợp các phương pháp được xác định trước được sử dụng để làm phong phú các lớp của bạn. Họ bắt đầu và kết thúc với các dấu gạch dưới gấp đôi. are a set of predefined methods used to enrich your classes. They start and end with double underscores.

Theo tài liệu chính thức,

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 được sử dụng để tính toán đại diện chuỗi chính thức của một đối tượng và thường được sử dụng để gỡ lỗi.

Cú pháp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
6

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
8

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
8

Trả về một chuỗi dưới dạng biểu diễn của đối tượng.

Lý tưởng nhất, đại diện phải là giàu thông tin và có thể được sử dụng để tạo lại một đối tượng có cùng giá trị.

Mã số

Hãy cùng làm một lớp,

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
9 và xác định phương pháp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1. Sau đó, chúng ta có thể gọi phương thức này bằng hàm Python tích hợp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
3.

# A simple Person class

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

def __repr__(self):

rep = 'Person(' + self.name + ',' + str(self.age) + ')'

return rep

# Let's make a Person object and print the results of repr()

person = Person("John", 20)

print(repr(person))

Người đóng góp

Nouman Abbasi

Bản quyền © 2022 Giáo dục, Inc. Tất cả quyền được bảo lưu

Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách sử dụng phương pháp Dunder Python

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 và sự khác biệt giữa các phương pháp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 và

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4.
: in this tutorial, you’ll learn how to use the Python

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 dunder method and the difference between the

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 and

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4 methods.

Giới thiệu về Phương pháp ma thuật Python class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = ageCode language: Python (python)1

Phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 Dunder xác định hành vi khi bạn chuyển một thể hiện của một lớp cho

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
3.

Phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 trả về biểu diễn chuỗi của một đối tượng. Thông thường,

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
9 trả về một chuỗi có thể được thực thi và mang lại cùng giá trị với đối tượng.

Nói cách khác, nếu bạn chuyển chuỗi được trả về của phương thức

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
0 cho hàm

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
1, bạn sẽ nhận được giá trị giống như

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
2. Hãy cùng xem một ví dụ.

Đầu tiên, xác định lớp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
9 với ba thuộc tính thể hiện

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
4,

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
5 và

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
6:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)

Thứ hai, tạo một thể hiện mới của lớp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
9 và hiển thị biểu diễn chuỗi của nó:

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)

Output:

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)

Theo mặc định, đầu ra chứa địa chỉ bộ nhớ của đối tượng

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
8. Để tùy chỉnh biểu diễn chuỗi của đối tượng, bạn có thể thực hiện phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 như thế này:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})'

Code language: Python (python)

Khi bạn chuyển một thể hiện của lớp

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
9 cho

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
3, Python sẽ tự động gọi phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1. Ví dụ:

person = Person("John", "Doe", 25) print(repr(person))

Code language: Python (python)

Output:

Person("John","Doe",25)

Code language: JavaScript (javascript)

Nếu bạn thực thi chuỗi trả về

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})'

Code language: Python (python)
3, nó sẽ trả về đối tượng

<__main__.Person object at 0x000001F51B3313A0>

Code language: HTML, XML (xml)
8.

Khi một lớp không thực hiện phương thức

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4 và bạn chuyển một phiên bản của lớp đó cho

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})'

Code language: Python (python)
6, Python trả về kết quả của phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 vì bên trong phương thức

person = Person('John', 'Doe', 25) print(repr(person))

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

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1:

Ví dụ:

person = Person('John', 'Doe', 25) print(person)

Code language: Python (python)

Output:

Person("John","Doe",25)

Code language: JavaScript (javascript)

Nếu một lớp thực hiện phương thức

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4, Python sẽ gọi phương thức

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4 khi bạn chuyển một thể hiện của lớp cho

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})'

Code language: Python (python)
6. Ví dụ:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})' def __str__(self): return f'({self.first_name},{self.last_name},{self.age})' person = Person('John', 'Doe', 25) # use str() print(person) # use repr() print(repr(person))

Code language: Python (python)

Output:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
0

person = Person('John', 'Doe', 25) print(repr(person))Code language: Python (python)4 vs class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = ageCode language: Python (python)1

Sự khác biệt chính giữa phương pháp

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4 và

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 là đối tượng dự định.

Phương thức

person = Person('John', 'Doe', 25) print(repr(person))

Code language: Python (python)
4 trả về một biểu diễn chuỗi của một đối tượng có thể đọc được trong khi phương thức

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

Code language: Python (python)
1 trả về một biểu diễn chuỗi của một đối tượng có thể đọc được bằng máy.

Bản tóm tắt

  • Thực hiện phương thức

    class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

    Code language: Python (python)
    1 để tùy chỉnh biểu diễn chuỗi của một đối tượng khi

    class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

    Code language: Python (python)
    3 được gọi trên nó.
  • person = Person('John', 'Doe', 25) print(repr(person))

    Code language: Python (python)
    4 gọi

    class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age

    Code language: Python (python)
    1 trong nội bộ theo mặc định.

Bạn có thấy hướng dẫn này hữu ích không?

Mục đích của việc xác định các hàm __ str __ và __ repr __ trong một lớp làm thế nào hai hàm khác nhau?

Câu lệnh in và hàm tích hợp str () sử dụng __str__ để hiển thị biểu diễn chuỗi của đối tượng trong khi hàm tích hợp repr () sử dụng __repr__ để hiển thị đối tượng.

Việc sử dụng __ str __ trong Python là gì?

Phương thức __str__ trong Python đại diện cho các đối tượng lớp dưới dạng chuỗi - nó có thể được sử dụng cho các lớp.Phương pháp __str__ nên được xác định theo cách dễ đọc và xuất ra tất cả các thành viên của lớp.Phương pháp này cũng được sử dụng như một công cụ gỡ lỗi khi các thành viên của một lớp cần được kiểm tra.

Là repr () một python được xây dựng

Sự định nghĩa.Hàm tích hợp python repr () trả về biểu diễn có thể in của đối tượng được chỉ định dưới dạng chuỗi.built-in function returns the printable representation of the specified object as a string.

__ repr __ sqlalchemy là gì?

Hàm __repr__ được xác định bởi người thiết kế thuộc loại, để cung cấp một phương tiện cho người dùng thuộc loại để biểu thị các giá trị của loại đó một cách rõ ràng, với một chuỗi.provide a means for users of the type to represent values of that type unambiguously, with a string.