Python对象属性管理

  • _dict_
  • _slots_
  • 属性管理
    hasattr()函数
    getattr()函数
    setattr()函数
    delattr()函数

Python下一切皆对象,每个对象都有多个属性(attribute),Python对属性有一套统一的管理方案。dict是用来存储对象属性的一个字典,其键为属性名,值为属性的值。

_dict_

class Spring(object):
    #season 为类的属性
    season = "the spring of class"

>>> Spring.__dict__
mappingproxy({'__module__': '__main__', 'season': 'the spring of class', '__dict__': <attribute '__dict__' of 'Spring' objects>, '__weakref__': <attribute '__weakref__' of 'Spring' objects>, '__doc__': None})

>>> Spring.__dict__['season']
'the spring of class'
>>> Spring.season
'the spring of class'

Spring._dict_['season']就是访问类属性,同时通过.号也可以访问该类属性

# 类实例化
s = Spring()
>>>s.__dict__
{}

实例属性的dict是空的,因为season是属于类属性

>>> s.season
'the spring of class'

s.season是指向了类属性中Spring.season

#建立实例对象
>>> s.season = 'the spring of instance'
>>> s.__dict__
{'season': 'the spring of instance'}

这样实例属性里面就不空了,这时候建立的s.season属性与上面s.season(类属性)重名,并且把原来的“遮盖了”。

#查看类属性
>>> Spring.season
'the spring of class'
>>> Spring.__dict__['season']
'the spring of class'

由上我们看到Spring类season属性并没有受到实例影响,其原因是实例s对象建立时s.season未初始化则会自动指向类属性,如果实例属性被修改那么就不指向类属性,同时类属性也不会受到影响

#定义其它实例属性
>>> s = Spring()
>>> s.lang = 'python'
>>> Spring.lang
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    Spring.lang
AttributeError: type object 'Spring' has no attribute 'lang'

上明错误表面 实例对象添加内容,不是类属性

#定义一个类
>>> class Spring:
    def tree(self,x):
        self.x = x
        print(self.x)

>>> Spring.__dict__['tree']
<function Spring.tree at 0x036E4AE0>

在类属性是存在tree方法

>>> s.__dict__['tree']
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    s.__dict__['tree']
KeyError: 'tree'

但是实例对象sdict 方法里不存在tree方法

>>> s.tree('123')
123

但是s能调用tree方法,其原因是s.tree指向了Spring.tree方法。 实例s与self新建了对应关系,两者是一个外一个内,在方法中self.x = x 。 将x值赋给了self.x,也就是实例应该拥有这么一个属性。self相当于java中this指针。

>>> s.__dict__
{'x': '123'}

即实例方法(s.tree('123'))的第一个参数(self,但没有写出来)绑定实例s,透过self.x来设定值,给s.dict添加属性值

换一个角度来看

class Spring:
    def tree(self,x):
        print(x)

这个方法没有self.x = x 直接输出x

>>> s = Spring()
>>> s.tree('123')
123
>>> s.__dict__
{}

再看一个例子

class Person(object):                      
    name = 'python'                        
    age = 18                               

    def __init__(self):                    
        self.sex = 'boy'                   
        self.like = 'papapa'               

    @staticmethod                          
    def stat_func():                       
        print 'this is stat_func'          

    @classmethod                           
    def class_func(cls):                   
        print 'class_func'                 


person = Person()                          
print 'Person.__dict__: ', Person.__dict__ 
print 'person.__dict__: ', person.__dict__

运行结果:

Person.__dict__:  {'__module__': '__main__', 'name': 'python', '__init__': <function __init__ at 0x000000000385B518>, 'class_func': <classmethod object at 0x0000000003847F78>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'age': 18, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'stat_func': <staticmethod object at 0x00000000037CFAF8>}
person.__dict__:  {'like': 'papapa', 'sex': 'boy'}

由此可见, 类的普通方法、类方法、静态方法、全局变量以及一些内置的属性都是放在类对象dict里

而实例对象中存储了一些self.xxx的一些东西

在类的继承中,子类有自己的dict, 父类也有自己的dict,子类的全局变量和方法放在子类的dict中,父类的放在父类dict中。

class Person(object):                          
    name = 'python'                            
    age = 18                                   

    def __init__(self):                        
        self.sex = 'boy'                       
        self.like = 'papapa'                   

    @staticmethod                              
    def stat_func():                           
        print 'this is stat_func'              

    @classmethod                               
    def class_func(cls):                       
        print 'class_func'                     


class Hero(Person):                            
    name = 'super man'                         
    age = 1000                                 

    def __init__(self):                        
        super(Hero, self).__init__()           
        self.is_good = 'yes'                   
        self.power = 'fly'                     


person = Person()                              
print 'Person.__dict__: ', Person.__dict__     
print 'person.__dict__: ', person.__dict__     

hero = Hero()                                  
print 'Hero.__dict__: ', Hero.__dict__         
print 'hero.__dict__: ', hero.__dict__

运行结果:

Person.__dict__:  {'__module__': '__main__', 'name': 'python', '__init__': <function __init__ at 0x000000000374B518>, 'class_func': <classmethod object at 0x0000000003750048>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'age': 18, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'stat_func': <staticmethod object at 0x0000000003737FD8>}
person.__dict__:  {'like': 'papapa', 'sex': 'boy'}
Hero.__dict__:  {'age': 1000, '__doc__': None, '__module__': '__main__', '__init__': <function __init__ at 0x000000000374B668>, 'name': 'super man'}
hero.__dict__:  {'is_good': 'yes', 'like': 'papapa', 'power': 'fly', 'sex': 'boy'}

从运行结果可以看出,类对象的dict虽然没有继承父类的,但是实例对象继承了父类的实例属性

_slots_

现在我们终于明白了,动态语言与静态语言的不同

  • 动态语言:可以在运行的过程中,修改代码

  • 静态语言:编译时已经确定好代码,运行过程中不能修改

如果我们想要限制实例的属性怎么办?比如,只允许对Person实例添加name和age属性。

为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的slots变量,来限制该class实例能添加的属性:

class Person:
    __slots__ = ("name", "age")
    def __init__(self,name,age):
        self.name = name
        self.age = age

p = Person("老王",20)
p.score = 100

输出

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/test/app.py", line 8, in <module>
    p.score = 100
AttributeError: 'Person' object has no attribute 'score'

注意: 使用slots要注意,slots定义的属性仅对当前类实例起作用,对继承的子类是不起作用的

class Person:
    __slots__ = ("name", "age")
    def __init__(self,name,age):
        self.name = name
        self.age = age

class GoodPerson(Person):
    pass

p = GoodPerson("老王",20)
p.score = 100

当你定义 slots 后,Python就会为实例使用一种更加紧凑的内部表示。 实例通过一个很小的固定大小的数组来构建,而不是为每个实例定义一个字典。所以slots是创建大量对象时节省内存的方法。slots的副作用是作为一个封装工具来防止用户给实例增加新的属性。 尽管使用slots可以达到这样的目的,但是这个并不是它的初衷。

属性管理

hasattr()函数

hasattr()函数用于判断对象是否包含对应的属性

语法:

  hasattr(object,name)

参数:

  object--对象

  name--字符串,属性名

返回值:

  如果对象有该属性返回True,否则返回False

示例:

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def people_info(self):
        print('%s is xxx' %(self.name))

obj=People('aaa')

print(hasattr(People,'country'))
#返回值:True
print('country' in People.__dict__)
#返回值:True
print(hasattr(obj,'people_info'))
#返回值:True
print(People.__dict__)
##{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10205d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}

1.3.2. getattr()函数

描述:

  getattr()函数用于返回一个对象属性值

语法:

  getattr(object,name,default)

参数:

  object--对象

  name--字符串,对象属性

  default--默认返回值,如果不提供该参数,在没有对于属性时,将触发AttributeError。

返回值:

  返回对象属性值
class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def people_info(self):
        print('%s is xxx' %(self.name))

obj=getattr(People,'country')
print(obj)
#返回值China
#obj=getattr(People,'countryaaaaaa')
#print(obj)
#报错
# File "/getattr()函数.py", line 32, in <module>
#     obj=getattr(People,'countryaaaaaa')
# AttributeError: type object 'People' has no attribute 'countryaaaaaa'
obj=getattr(People,'countryaaaaaa',None)
print(obj)
#返回值None

1.3.3. setattr()函数

描述:

  setattr函数,用于设置属性值,该属性必须存在

语法:

  setattr(object,name,value)

 参数:

  object--对象

  name--字符串,对象属性

  value--属性值

返回值:

  无
class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def people_info(self):
        print('%s is xxx' %(self.name))

obj=People('aaa')

setattr(People,'x',111) #等同于People.x=111
print(People.x)

#obj.age=18
setattr(obj,'age',18)
print(obj.__dict__)
#{'name': 'aaa', 'age': 18}
print(People.__dict__)
#{'__module__': '__main__', 'country': 'China', '__init__': <function People.__init__ at 0x1007d5620>, 'people_info': <function People.people_info at 0x10215d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, 'x': 111}

1.3.4. delattr()函数

描述:

  delattr函数用于删除属性

  delattr(x,'foobar)相当于del x.foobar

语法:

  setattr(object,name)

参数:

  object--对象

  name--必须是对象的属性

返回值:

  无

示例:

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def people_info(self):
        print('%s is xxx' %(self.name))

delattr(People,'country') #等同于del People.country
print(People.__dict__)
{'__module__': '__main__', '__init__': <function People.__init__ at 0x1006d5620>, 'people_info': <function People.people_info at 0x10073d1e0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
补充示例

class Foo:
    def run(self):
        while True:
            cmd=input('cmd>>: ').strip()
            if hasattr(self,cmd):
                func=getattr(self,cmd)
                func()

    def download(self):
        print('download....')

    def upload(self):
        print('upload...')

# obj=Foo()
# obj.run()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容