-
Notifications
You must be signed in to change notification settings - Fork 23
/
__repr__method.py
55 lines (46 loc) · 2.04 KB
/
__repr__method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
################################################################################
#
# Program: __repr__ Method Example
#
# Description: Example of using the __repr__ magic method (i.e. dunder method)
# in Python to represent an object as a string in a formal, complete way that is
# suitable for debugging or programming purposes.
#
# YouTube Lesson: https://www.youtube.com/watch?v=LV27ZMm8TWk
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# A simple class for representing students
class Student:
# Student objects will have name and grade attributes
def __init__(self, name, grade):
self.name = name
self.grade = grade
# The __repr__ method is expected to return a string representation of the
# object that is suitable for programming or debugging purposes. Here we
# use an f-string to build and return a string that contains the object
# member variable values with the format: Student('name','grade')
def __repr__(self):
return f"Student('{self.name}','{self.grade}')"
# There is a similar magic method called __str__ that is intended for
# representing the object as a string in a more informal and human-readable
# way. We could also define this method.
#
# def __str__(self):
# Create a Student object
mary = Student("Mary", "A+")
# The built-in repr() function will call our __repr__ method above and return
# the string it produces, which we output...
mary_string = repr(mary)
print(mary_string)
# The string returned be our __repr__ method can be evaluated as Python code
# using the built in eval() function, which will create a Student object.
# We assign the resulting object to mary_object and verify it's type and
# output the attribute name.
mary_object = eval(mary_string)
print(type(mary_object))
print(mary_object.name)
# If we do not define an __str__ method, the print() function will use our
# __repr__ method to convert mary_object to a string to be printed.
print(mary_object)