美文网首页
Python魔法方法-__dict__

Python魔法方法-__dict__

作者: 莫忘初心_倒霉熊 | 来源:发表于2020-02-19 16:16 被阅读0次

__dict__

类或对象中的所有属性
类的实例属性属于对象;类中的类属性和方法等属于类,即:

class Province(object):
    country = 'china'

    def __init__(self,name,count):
        self.name = name
        self.count = count

    def func(self,*args,**kwargs):
        print('func')

# 获取类的属性,即:类属性,方法
print(Province.__dict__)
# 输出:
# {'__module__': '__main__',
#  'country': 'china',
#  '__init__': <function Province.__init__ at 0x10372b4d0>,
#  'func': <function Province.func at 0x10372b560>,
#  '__dict__': <attribute '__dict__' of 'Province' objects>,
#  '__weakref__': <attribute '__weakref__' of 'Province' objects>,
#  '__doc__': None}

# 获取对象obj1的属性
obj1 = Province("山东",10000)
print(obj1.__dict__)
# 输出:
# {'name': '山东',
#  'count': 10000}

# 获取对象obj2的属性
obj2 = Province("江西",20000)
print(obj2.__dict__)
# 输出:
# {'name': '江西',
#  'count': 20000}

相关文章

网友评论

      本文标题:Python魔法方法-__dict__

      本文链接:https://www.haomeiwen.com/subject/vqbafhtx.html