locals()
代码实验:
# locals()函数使用示例 result = locals() print("nn变量创建前...") print("变量类型:", type(result), "变量长度:",len(result)) print(result) #---------------------------------# aString = "abc" bInt = 22 cFloat = 3.14 dList = [0, 1, 2, 3] print("nn变量创建后...") result = locals() print("变量类型:", type(result), "变量长度:",len(result)) print(result)
控制台结果输出:
Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 加载个人及系统配置文件用了 750 毫秒。 (base) PS C:UserschenxuqiDesktopnewstest> & 'D:Python37python.exe' 'c:Userschenxuqi.vscodeextensionsms-python.python-2021.10.1336267007pythonFileslibpythondebugpylauncher' '11445' '--' 'c:UserschenxuqiDesktopnewstesttest_01.py' 变量创建前... 变量类型:变量长度: 10 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001CAECD5F048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_01.py', '__cached__': None, 'result': {...}} 变量创建后... 变量类型: 变量长度: 14 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001CAECD5F048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_01.py', '__cached__': None, 'result': {...}, 'aString': 'abc', 'bInt': 22, 'cFloat': 3.14, 'dList': [0, 1, 2, 3]} (base) PS C:UserschenxuqiDesktopnewstest>
globals()
代码实验:
# 测试globals()函数 print("nn变量创建前...") result = globals() print("变量类型:", type(result), "变量长度:",len(result)) print(result) # --------------------------------- # globals()["age"] = 22 print("通过globals函数创建变量age, age =", age) globals()["age"] = '二十二岁' print("通过globals函数修改变量age, age =", age) AGE = 33 print("AGE =", AGE) globals()["AGE"] = '八十八岁' print("通过globals函数修改变量AGE, AGE =", AGE) # --------------------------------- # aString = "abc" bInt = 22 cFloat = 3.14 dList = [0, 1, 2, 3] print("nn变量创建后...") # result = globals() print("变量类型:", type(result), "变量长度:",len(result)) print(result)
控制台结果输出:
Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 加载个人及系统配置文件用了 736 毫秒。 (base) PS C:UserschenxuqiDesktopnewstest> & 'D:Python37python.exe' 'c:Userschenxuqi.vscodeextensionsms-python.python-2021.10.1336267007pythonFileslibpythondebugpylauncher' '14902' '--' 'c:UserschenxuqiDesktopnewstesttest_02.py' 变量创建前... 变量类型:变量长度: 10 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002385D43F048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_02.py', '__cached__': None, 'result': {...}} 通过globals函数创建变量age, age = 22 通过globals函数修改变量age, age = 二十二岁 AGE = 33 通过globals函数修改变量AGE, AGE = 八十八岁 变量创建后... 变量类型: 变量长度: 16 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002385D43F048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_02.py', '__cached__': None, 'result': {...}, 'age': '二十二岁', 'AGE': '八十八岁', 'aString': 'abc', 'bInt': 22, 'cFloat': 3.14, 'dList': [0, 1, 2, 3]} (base) PS C:UserschenxuqiDesktopnewstest>
vars([object])
实验代码展示:
# 测试vars()函数 print("变量长度:",len(vars())) print(vars()) print('类定义之前'.center(50,'-')) result = vars() print("变量类型:", type(result), "变量长度:",len(result)) print(result) # ----------------------------------------------------------- # 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) ) ) class Student(Person): '''定义基类Person的子类Student''' def __init__(self, name, age, grade): '''初始化父类的属性name和age''' super().__init__(name, age) self.grade = grade def printInfo(self): print( '---我是一个实例对象, 属于类型:{0:^35} ---我的名字是: {1:{3}<8} ---我的年龄是: {2:<5}'.format( str(self.__class__), str(self.name), str(self.age), chr(12288) ) ) # result = vars() print('类定义之后'.center(50,'-')) print("变量类型:", type(result), "变量长度:",len(result)) print(result) # ----------------------------------------------------------- # per = Person('人物', 18) stu = Student("学生", 22, '二年级-sophomore') print('实例对象创建之后'.center(50,'-')) print("变量类型:", type(result), "变量长度:",len(result)) print(result) print() print('实例对象创建之后'.center(50,'-')) print('实例对象per的属性名与属性值'.center(50,'-')) result = vars(per) print("变量类型:", type(result), "变量长度:",len(result)) print(result) print() print('实例对象stu的属性名与属性值'.center(50,'-')) result = vars(stu) print("变量类型:", type(result), "变量长度:",len(result)) print(result)
控制台结果输出:
Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 加载个人及系统配置文件用了 730 毫秒。 (base) PS C:UserschenxuqiDesktopnewstest> & 'D:Python37python.exe' 'c:Userschenxuqi.vscodeextensionsms-python.python-2021.10.1365161279pythonFileslibpythondebugpylauncher' '5753' '--' 'c:UserschenxuqiDesktopnewstesttest_03.py' 变量长度: 9 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002091CDDF048>, '__spec__': None, '__annotations__': {}, '__builtins__':, '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_03.py', '__cached__': None} ----------------------类定义之前----------------------- 变量类型: 变量长度: 10 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002091CDDF048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_03.py', '__cached__': None, 'result': {...}} ----------------------类定义之后----------------------- 变量类型: 变量长度: 12 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002091CDDF048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_03.py', '__cached__': None, 'result': {...}, 'Person': , 'Student': } ---------------------实例对象创建之后--------------------- 变量类型: 变量长度: 14 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002091CDDF048>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'c:\Users\chenxuqi\Desktop\news\test\test_03.py', '__cached__': None, 'result': {...}, 'Person': , 'Student': , 'per': <__main__.Person object at 0x000002091D02E148>, 'stu': <__main__.Student object at 0x000002091D02E0C8>} ---------------------实例对象创建之后--------------------- -----------------实例对象per的属性名与属性值------------------ 变量类型: 变量长度: 2 {'name': '人物', 'age': 18} -----------------实例对象stu的属性名与属性值------------------ 变量类型: 变量长度: 3 {'name': '学生', 'age': 22, 'grade': '二年级-sophomore'} (base) PS C:UserschenxuqiDesktopnewstest>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)