返回顶部
扩大
缩小

Zhang_derek

10.python3实用编程技巧进阶(五)

5.1.如何派生内置不可变类型并修其改实例化行为

修改实例化行为

# 5.1.如何派生内置不可变类型并修其改实例化行为

#继承内置tuple, 并实现__new__,在其中修改实例化行为
class IntTuple(tuple):
    def __new__(cls, iterable):
        #过滤掉元祖中不是int类型且小于0的元素
        f_it = (e for e in iterable if isinstance(e, int) and e > 0)
        return super().__new__(cls, f_it)

int_t = IntTuple([1, 8, -2, -3, 'abc', [4,5], 10])

print(int_t)    #(1, 8, 10)

5.2.如何为创建大量实例节省内存

定义类的__slots__属性,声明实例有哪些属性(关闭动态绑定)

#5.2.如何为创建大量实例节省内存

class Player1:
    def __init__(self,uid, name, level):
        self.uid = uid
        self.name = name
        self.level = level


class Player2:
    #定义类的__slots__属性,声明实例有哪些属性(关闭动态绑定)
    __slots__ = ['uid', 'name', 'level']
    def __init__(self,uid, name, level):
        self.uid = uid
        self.name = name
        self.level = level

p1 = Player1(1,'derek',20)
p2 = Player2(2,'jack',50)
print(p1.name)
print(p2.name)
#可以为实例动态添加属性,但是这样会消耗内存
p1.age = 18
print(p1.__dict__['age'])    #18

#用__slot__声明了实例有哪些属性后,则不能动态为实例添加属性
#p2.age = 22    #AttributeError: 'Player2' object has no attribute 'age'

 5.3.如何创建可管理的对象属性

一般写法

#5.3.如何创建可管理的对象属性

class Student():
    def __init__(self,score):
        self.score = score

    def get_score(self):
        return self.score

    def set_score(self, score):
        if not isinstance(score, int):
            raise TypeError("wrong type")
        self.score = score

s = Student(70)
print(s.get_score())

s.set_score(80)
print(s.score)

用property装饰器

#5.3.如何创建可管理的对象属性

class Student():
    def __init__(self,score):
        self.score = score

    @property
    def value(self):
        return self.score

    @value.setter
    def value(self, score):
        if not isinstance(score, int):
            raise TypeError("wrong type")
        self.score = score

s = Student(70)
print(s.value)

s.value = 80
print(s.value)

 

posted on 2019-08-20 23:27 zhang_derek 阅读(...) 评论(...) 编辑 收藏

导航