修改前
class student(object):
def __init__(self, name, age):
self.name = name
self.age = age
one = student('Tom', 15)
two = student('Jerry', 14)
print(one)
print(two)
修改前的输出
<__main__.student object at 0x000001CCD89F8F28>
<__main__.student object at 0x000001CCD89F8F98>
修改后
# -*- coding: utf-8 -*-
"""
@Time : 2020/5/7 23:11
@Auth : jian
@File :demo.py
@IDE :PyCharm
@Motto:You cannot improve your past, but you can improve your future.
"""
class student(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} {self.age}"
one = student('Tom', 15)
two = student('Jerry', 14)
print(one)
print(two)
修改后的输出
Tom 15
Jerry 14
注解:
修改此方法可以直接打印对象的内容,你可以根据自己想要的输出风格来定义此方法