class Student: def __init__(self,name,age): self.name=name self.age=age def __str__(self): #object中有__str__()方法,用于对于 ‘对象的描述’ return '我的名字是{0},今年{1}岁'.format(self.name,self.age) stu=Student('张三',20) print(dir(stu)) #查看指定对象所有属性 print(stu) #默会调用__str__()这样的方法 print(type(stu))
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/demon1/demo46.py ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name'] 我的名字是张三,今年20岁 <class '__main__.Student'> 进程已结束,退出代码0