Python学习札记(三十六) 面向对象编程 Object Oriented Program 7 __slots__

本文介绍Python中如何动态地为对象绑定属性和方法,并通过__slots__特性限制实例可以添加的属性,增强代码的安全性和效率。

参考:slots

NOTE

1.动态语言灵活绑定属性及方法。

#!/usr/bin/env python3

class MyClass(object):
    def __init__(self):
        pass

def func(obj):
    print(obj.name, obj.age)

def main():
    h = MyClass()
    h.name = 'Chen'
    h.age = '20'
    func(h)

if __name__ == '__main__':
    main()

给对象h绑定了属性name和age。

sh-3.2# ./oop7.py 
Chen 20

绑定一个新的方法:

from types import MethodType

def f(self):
    print('I\'m new here!')

h.f = MethodType(f, h) # new method
h.f()
I'm new here!

但是这种绑定的方法并不存在于新建的对象:

    h1 = MyClass()
    h1.f()
Traceback (most recent call last):
  File "./oop7.py", line 28, in <module>
    main()
  File "./oop7.py", line 25, in main
    h1.f()
AttributeError: 'MyClass' object has no attribute 'f'

给类绑定一个方法,解决这个问题:

    MyClass.f = f

    h1 = MyClass()
    h1.f()
I'm new here!

通常情况下,上面的f方法可以直接定义在class中,但动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现。

2.__slots__

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

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

#!/usr/bin/env python3

class MyClass(object):
    """docstring for MyClass"""
    __slots__ = ('name', 'age')
    def __init__(self):
        super(MyClass, self).__init__()
        pass

def main():
    h = MyClass()
    h.name = 'Chen'
    h.age = 20
    h.city = 'FuZhou'

if __name__ == '__main__':
    main()
sh-3.2# ./oop8.py 
Traceback (most recent call last):
  File "./oop8.py", line 17, in <module>
    main()
  File "./oop8.py", line 14, in main
    h.city = 'FuZhou'
AttributeError: 'MyClass' object has no attribute 'city'

__slots__用tuple定义允许绑定的属性名称,由于'city'没有被放到__slots__中,所以不能绑定city属性。

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

#!/usr/bin/env python3

class MyClass(object):
    """docstring for MyClass"""
    __slots__ = ('name', 'age')
    def __init__(self):
        super(MyClass, self).__init__()
        pass

class Student(MyClass):
    """docstring for Student"""
    def __init__(self):
        super(Student, self).__init__()
        pass
        

def main():
    h = MyClass()
    h.name = 'Chen'
    h.age = 20
    # h.city = 'FuZhou'

    h1 = Student()
    h1.name = 'Chen'
    h1.age = 20
    h1.city = 'FuZhou'

    print(h1.name, h1.age, h1.city)

if __name__ == '__main__':
    main()
sh-3.2# ./oop8.py 
Chen 20 FuZhou

2017/3/2

转载于:https://www.cnblogs.com/qq952693358/p/6493135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值