美文网首页
python——类装饰器,垃圾回收

python——类装饰器,垃圾回收

作者: Jalynn葸 | 来源:发表于2018-05-11 10:26 被阅读66次
类装饰器
class Test(object):
    def __init__(self, func):
        super(Test, self).__init__()
        print("----初始化----")
        print("func name is %s"%func.__name__)
        self.__func = func
    def __call__(self):
        print("装饰器中的功能")
        self.__func()
#就相当于test = Test(test)
@Test
def test():
    print("---test----")

test()
元类

类也是对象

class Person(object):
    num = 0
    print("xxxxxxxxxxx")
    def __init__(self):
        self.name = "abc"
print(Person)

xxxxxxxxxxx
<class '__main__.Person'>

可以动态的创建类
type可以用来动态创建类,也可以测试数据类型

>>> Test = type("Test",(),{})
>>> t = Test()
>>> type(t)
<class '__main__.Test'>

type创建类时,创建属性

>>> Person = type("Person",(),{"num":0})
>>> p = Person()
>>> p.num
0

用type创建类时,添加方法

>>> def printNum(self):
...     print("%d"%self.num)
... 
>>> Test1 = type("Test1",(),{"printNum":printNum})
>>> t = Test1()
>>> t.num = 199
>>> t.printNum()
199
class Animal(object):
    def eat(self):
        print("----eat----")
class Dog(Animal):
    def __init__(self):
        super(Dog, self).__init__()

wangcai = Dog()
wangcai.eat()
Cat = type("Cat",(Animal,),{})
cat = Cat()
cat.eat()
print(type(Dog.__class__))
-------------------
----eat----
----eat----
<class 'type'>
metaclass属性
GC:垃圾回收机制

引用计数器的缺点就是循环引用

隔代回收

python 已经默认开通了GC

import gc
#获取当前自动执行垃圾回收的计数器
>>> gc.get_count()
(365, 4, 1)
#设置自动执行垃圾回收的频率
>>> gc.set_threshold(20,2,33)
>>> gc.get_threshold()
(20, 2, 33)
导致引用计数器+1的情况

1、对象被创建
2、对象被引用
3、对象被作为参数,传入到一个函数中

导致引用计数器-1的情况

1、对象被显示销毁
2、对象所在容器销毁

>>> import sys
>>> sys.getrefcount(a)

默认情况下gc是开启的,也可以手动关闭和开启。

import gc
gc.enable()
gc.disable()
gc.collect()//手动清理垃圾
gc.garbage//一个保存已经清理的垃圾的列表
内建属性
image.png
getattribute
访问属性时会先被调用
class Itcast(object):
#当init被调用时,attribute会被触发
    def __init__(self, subject1):
        super(Itcast, self).__init__()
        self.subject1 = subject1
        self.subject2 = 'cpp'
    def __getattribute__(self, obj):
        if obj == 'subject1':
            print('log subject1')
            return 'xjx'
        else:
            return object.__getattribute__(self,obj)

    def show(self):
        print('this is Itcast')

s = Itcast("python")
print(s.subject1)
print(s.subject2)
range()

在getattribute里禁止使用 self.xxx ; 否则程序会崩掉。iOS里也有这种情况。
在python2里range会生成一个列表,python3什么都不会做
xrange只会生成空间但不会生成所有列表的内容

map

是一种映射关系

>>> map(lambda x:x*x,[1,2,3])
<map object at 0x102a9eb70>
>>> ret = map(lambda x:x*x,[1,2,3])
>>> for temp in ret:
...     print(temp)
1
4
9
#map(lambda x, y : x+y, [1,2,3],[4,5,6])
filter

筛选和过滤

filter(lambda x:x%2,[1,2,3,4])
[1,3]
reduce
reduce(lambda x,y:x+y,[1,2,3,4],5)
15
sort
>>> a = [22,34,5,66,6,87,654,1,44,33]
>>> sorted(a)
[1, 5, 6, 22, 33, 34, 44, 66, 87, 654]
集合set

可以求交并补
A&B交集、A|B并集、X-Y差集、A^B对称差集

>>> y = set('abcd')
>>> y
{'c', 'a', 'd', 'b'}             
functools
>>> import functools
>>> dir(functools)
['MappingProxyType', 'RLock', 
'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', 'WeakKeyDictionary', 
'_CacheInfo', '_HashedSeq', '__all__', 
'__builtins__', '__cached__', '__doc__', 
'__file__', '__loader__', '__name__', 
'__package__', '__spec__', '_c3_merge', 
'_c3_mro', '_compose_mro', '_convert', 
'_find_impl', '_ge_from_gt', '_ge_from_le', 
'_ge_from_lt', '_gt_from_ge', '_gt_from_le', 
'_gt_from_lt', '_le_from_ge', '_le_from_gt',
 '_le_from_lt', '_lru_cache_wrapper', 
'_lt_from_ge', '_lt_from_gt', '_lt_from_le', 
'_make_key', 'cmp_to_key', 
'get_cache_token', 'lru_cache', 'namedtuple', 
'partial', 'partialmethod', 'recursive_repr', 
'reduce', 'singledispatch', 'total_ordering', 
'update_wrapper', 'wraps']

相关文章

  • python——类装饰器,垃圾回收

    类装饰器 元类 类也是对象 可以动态的创建类type可以用来动态创建类,也可以测试数据类型 type创建类时,创建...

  • day22类装饰器

    类装饰器 1.1python是动态语言 添加类方法和静态方法 1.1.1运行的过程中删除属性、方法 1.1垃圾回收...

  • JVM(七)垃圾收集器

    1. 垃圾收集器概述 1.1 垃圾回收器与垃圾回收算法 垃圾回收算法分类两类,第一类算法判断对象生死算法,如引用计...

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • 解惑,从新认识python装饰器

    概念 python有两种装饰器: 函数装饰器(function decorators) 类装饰器(class de...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • TypeScript: 类的装饰器(三)

    带参数的类的装饰器 学习 python 的同学应该知道,python 中也有装饰器,而且 python 中的众多框...

  • JVM常见垃圾回收器介绍

    垃圾回收器简介 在新生代和老年代进行垃圾回收的时候,都是要用垃圾回收器进行回收的,不同的区域用不同的垃圾回收器。分...

  • JVM调优之垃圾定位、垃圾回收算法、垃圾处理器对比

    谈垃圾回收器之前,要先讲讲垃圾回收算法,以及JVM对垃圾的认定策略,JVM垃圾回收器是垃圾回收算法的具体实现,了解...

  • ZGC设计与实现-出版

    目录如下:前言第1章垃圾回收器概述 11.1 垃圾回收算法 21.2 JVM垃圾回收器 21.2.1 串行回收 3...

网友评论

      本文标题:python——类装饰器,垃圾回收

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