参考链接: object.__dict__
参考链接: instance.__class__
参考链接: class.__bases__
参考链接: definition.__name__
参考链接: definition.__qualname__
代码展示:
# class Person():
# class Person(object):
# class Person:
class Person: # class Person(object): # class Person: # class Person(): 这三种写法都是可以的
'''定义基类Person'''
def __init__(self, name, age):
'''初始化属性name和age'''
self.name = name
self.age = age
def printInfo(self):
print(
'---我是一个实例对象, 属于类型:{0:^35} ---我的名字是: {1:{3}<8} ---我的年龄是: {2:<5}'.format(
str(self.__class__), str(self.name), str(self.age), chr(12288)
)
)
if __name__ == "__main__":
print()
per = Person('林麻子',15)
per.printInfo()
print('-'*50)
print('per.__class__: ', per.__class__)
print('per.__dict__: ', per.__dict__)
print('Person.__name__: ', Person.__name__)
print('Person.__bases__: ', Person.__bases__)
print('Person.__qualname__: ', Person.__qualname__)
控制台输出结果:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
加载个人及系统配置文件用了 1003 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> conda activate ssd
(ssd) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> & 'D:\Anaconda3\envs\ssd\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\launcher' '63409' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\test4cxq\test21.py'
---我是一个实例对象, 属于类型: <class '__main__.Person'> ---我的名字是: 林麻子 ---我的年龄是: 15
--------------------------------------------------
per.__class__: <class '__main__.Person'>
per.__dict__: {'name': '林麻子', 'age': 15}
Person.__name__: Person
Person.__bases__: (<class 'object'>,)
Person.__qualname__: Person
(ssd) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq>