g开头的

'''
getattr() 函数用于返回一个对象属性值。
getattr(object, name[, default])
object -- 对象。
name -- 字符串,对象属性。
default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。
返回对象属性值。
'''
class animal(object):
   color = 'blue'
animal = animal()
print(getattr(animal, 'color')) # 获取属性 color 值
# print(getattr(animal, 'name')) # 属性 name 不存在,触发异常
print(getattr(animal, 'name', 'lidihuo')) # 属性 name 不存在,但设置了默认值
print('-'*10,"分割线","-"*10)

'''
globals() 函数会以字典类型返回当前位置的全部全局变量
globals()
无参数
返回全局变量的字典。
'''
a = 'hello world'
print(globals())
posted @ 2021-09-12 17:46  索匣  阅读(39)  评论(0编辑  收藏  举报