# 构造方法 申请一个空间# 析构方法 释放一个空间

# 某个对象借用了操作系统的资源,还要通过析构方法归还回去:文件资源 网络资源

# 垃圾回收机制

class A:    def __del__(self):        #构析方法  del   A的对象会自动出发这个方法        print('哈哈哈哈')a=A()del aprint(a)

# 处理文件的class File():    def __init__(self,file_path):        self.f=open(file_path)        self.name='alex'    def read(self):        self.f.read(1024)    def __del__(self): #是去归还/释放一些在创建对象的时候借用的一些资源        # del 对象的时候  程序员触发        #python 解释器的垃圾回收机制 回收这个对象所占的内存的时候 python自动触发的        self.f.close()f=File('文件名')f.read()del f    # del 对象的时候  程序员触发#不管是主动还是被动,这个f 对象总会被清理掉,被清理掉就出发__del__方法,触发这个方法就会归换操作系统的文件资源

# python 解释器在内存部就能搞定的事# 申请一块儿空间 操作系统分配给你的# 在这一块儿空间之内的所有事儿 归你的python解释器来管理## a=1# del a# 对象--> 内存

# f = open('wenjian')  # python --> 操作系统 --> 硬盘里的文件 --> 文件操作符# f.close()# # 文件操作符# del f

# item系列 和对象使用[]访问值有联系

ojb={'k':"v"}print(ojb)print(ojb['k'])

# 在内置的模块中,有一些特殊的方法,要求对象必须实现__getitem__/__setitem__才能使用class B:    def __getitem__(self, item):        return getattr(self,item)    def __setitem__(self, key, value):        setattr(self,key,value*2)#self.key = v    def __delitem__(self, key):        delattr(self,key)b=B()

b['a']='c'# print(b['a'])del b['a']print(b.__dict__)

class A:    def __init__(self,li):        self.li=li    def __getitem__(self, item):        return self.li[item]    def __setitem__(self, key, value):        self.li[key]=value    def __delitem__(self, key):        self.li.pop(key)a=A(['111','222','ccc','dddd'])print(a.li[2])print(a[2])#  用这种a[2]='weew'print(a.li)del a[3]print(a.li)

# hash方法# 底层数据结构基于hash值寻址的优化操作# hash是一个算法# 能够把某一个要存在内存里的值通过一系列计算,# 保证不同值的hash结果是不一样的# '127647862861596'  ==> 927189778748# 对同一个值在多次执行python代码的时候hash值是不同# 但是对同一个值 在同一次执行python代码的时候hash值永远不变print(hash('abc')) 第一次 # 6048279107854451739   第二次 #-6924590381416277323print(hash('abc'))# 6048279107854451739     #-6924590381416277323print(hash('abc'))# 6048279107854451739     #-6924590381416277323print(hash('abc'))# 6048279107854451739     #-6924590381416277323print(hash('abc'))# 6048279107854451739     #-6924590381416277323print(hash('abc'))# 6048279107854451739     #-6924590381416277323

# 字典的寻址  - hash算法# d = {'key':'value'}# hash - 内置函数

# set集合# se = {1,2,2,3,4,5,'a','b','d','f'}# print(se)

# d = {'key':'v1','key':'v2'}# print(d['key'])# hash(obj) #obj内部必须实现了__hash__方法

# __eq__

class A:    def __init__(self,name,age):        self.name = name        self.age = age    def __eq__(self, other):        if self.name== other.name and self.age ==other.age:            return Truea = A('alex',83)aa = A('alex',83)aa2 = A('alex',83)aa3 = A('alex',83)aa4 = A('alex',83)aa5 = A('alex',83)aa6 = A('alex',83)print(a,aa)print(a==aa==aa2)     # ==这个语法 是完全和__eq__

# 面试题

# 一个类# 对象的属性 : 姓名 性别 年龄 部门# 员工管理系统# 内部转岗 python开发 - go开发# 姓名 性别 年龄 新的部门# alex None 83 python# alex None 85 luffy

# 1000个员工# 如果几个员工对象的姓名和性别相同,这是一个人# 请对这1000个员工做去重

class Employee:    def __init__(self,name,age,sex,partment):        self.name = name        self.age = age        self.sex = sex        self.partment = partment    def __hash__(self):        return  hash('%s%s'%(self.name,self.sex))    def __eq__(self, other):        if self.name==other.name and self.sex== other.sex:            return Trueemploy_lst = []for  i in range(200):    employ_lst.append(Employee('alex',i,'male','python'))for  i in range(200):    employ_lst.append(Employee('wusi',i,'male','python'))for  i in range(200):    employ_lst.append(Employee('taiba',i,'male','python'))employ_lst=set(employ_lst)for person in employ_lst:    print(person.__dict__)

# set集合的去重   先调用hash 再调用eq, eq不是每次都出发,只有hash 值相等才触发

__del__,item系列 ,hash方法,__eq__,的更多相关文章

  1. python 之前函数补充(__del__, item系列, __hash__, __eq__) , 以及模块初体验

    __str__ :  str(obj) ,  需求必须实现了 __str__, 要求这个方法的返回值必须是字符串  str  类型 __repr__ (意为原型输出):  是 __str__ 的备胎( ...

  2. item系列魔法方法

    class Foo: def __init__(self, name): self.name = name def __getitem__(self, item): print('getitem执行' ...

  3. python面向对象( item系列,__enter__ 和__exit__,__call__方法,元类)

    python面向对象进阶(下)   item系列 __slots__方法 __next__ 和 __iter__实现迭代器  析构函数 上下文管理协议 元类一.item系列 把对象操作属性模拟成字典的 ...

  4. [ python ] 反射及item系列

    反射 什么是反射? 通过字符串的形式操作对象相关属性.python中的事物都是对象: 关键方法: (1)getattr:获取属性 (2)setattr:设置属性 (3)hashattr:检测是否含有属 ...

  5. python 全栈开发,Day24(复习,__str__和__repr__,__format__,__call__,__eq__,__del__,__new__,item系列)

    反射: 使用字符串数据类型的变量名来使用变量 wwwh即what,where,why,how  这4点是一种学习方法 反射 :使用字符串数据类型的变量名来使用变量 1.文件中存储的都是字符串 2.网络 ...

  6. 1、__del__ 2、item系列 3、__hash__ 4、__eq__

    1.__del__   析构方法       释放一个空间之前之前 垃圾回收机制   2.item系列   和对象使用[ ]访问值有联系 __getitem__ __setitem__ __delit ...

  7. 面向对象进阶-item系列、__new__、__hash__、__eq__ (四)

    item系列 dic = {'k':'v'}# 对象 : 存储属性 和调用方法dic['k'] = 'v'# class Foo:#     def __init__(self,name,age,se ...

  8. 面向对象的进阶(item系列,__new__,__hash__,__eq__)

      面向对象的进阶(item系列,__new__,__hash__,__eq__) 一.item系列 getitem.setitem.delitem(操作过程达到的结果其实就是增删改查) class ...

  9. 实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例)

    实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例) 1.namedtuple:命名元组,可以创建一个没有方法只有属性的类 from collections import namedtup ...

随机推荐

  1. 8.1 服务器开发 API 函数封装,select 优化服务器和客户端

    #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <ne ...

  2. php curl批处理

    php模拟多线程用到curl库,这个库很强大,可以做好多事,比如模拟登陆,文件上传/下载,数据采集等. 下面是我的代码,很简单,有些还功能还不会用. <?php $node_urls=array ...

  3. [转载]redis持久化的两种操作RDB和AOF

    Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...

  4. 转-spring boot web相关配置

    spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...

  5. 树的遍历算法-只有一个变量T-递归和非递归

    void PostOrderTraverse(BTNode *T) { //就用到了一个变量T if(T==NULL) return; PostOrderTraverse(T->lchild); ...

  6. System.out.println()详解 和 HttpServletRequest 和 XMLHttpRequest

    System是一个类,位于java.lang这个包里面.out是这个System类的一个PrintStream类型静态属性.println()是这个静态属性out所属类PrintStream的方法. ...

  7. C条件编译

    #include <stdio.h> void main() { #ifdef AAA printf("find AAA defined\n"); #else prin ...

  8. Android 第三方分享中遇到的问题以及解决方式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/liuxian13183/article/details/36189343               ...

  9. 实战:mysql检查物理磁盘中的二进制日志文件是否有丢失

    版权声明:日常札记,乐于分享 https://blog.csdn.net/yangzhawen/article/details/32103555 场景:有时候由于磁盘损坏或人为原因错误删除了磁盘中的二 ...

  10. tomcat源码阅读之Server和Service接口解析

    tomcat中的服务器组件接口是Server接口,服务接口是Service,Server接口表示Catalina的整个servlet引擎,囊括了所有的组件,提供了一种优雅的方式来启动/关闭Catali ...