File tree Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change 81
81
调用对象的 \_ _ repr__ 方法,获得该方法的返回值,如下例子返回值为字符串
82
82
83
83
``` python
84
- In [1 ]: class Student ():
85
- ... : def __init__ (self ,id ,name ):
86
- ... : self .id = id
87
- ... : self .name = name
88
- ... : def __repr__ (self ):
89
- ... : return ' id = ' + self .id + ' , name = ' + self .name
90
- ... :
91
- ... :
84
+ class Student ():
85
+ def __init__ (self ,id ,name ):
86
+ self .id = id
87
+ self .name = name
88
+ def __repr__ (self ):
89
+ return ' id = ' + self .id + ' , name = ' + self .name
90
+ ```
92
91
93
- In [2 ]: xiaoming = Student(id = ' 001' ,name = ' xiaoming' )
92
+ ``` python
93
+ In [2 ]: xiaoming = Student(id = ' 1' ,name = ' xiaoming' )
94
94
95
95
In [3 ]: print (xiaoming)
96
- id = 0 01 , name = xiaoming
96
+ id = 1 , name = xiaoming
97
97
98
98
In [4 ]: ascii (xiaoming)
99
99
Out[4 ]: ' id = 001, name = xiaoming'
You can’t perform that action at this time.
0 commit comments