前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >测试开发进阶(七)

测试开发进阶(七)

作者头像
zx钟
发布2019-08-22 22:52:12
3870
发布2019-08-22 22:52:12
举报
文章被收录于专栏:测试游记测试游记

元类

新式类 旧式类

代码语言:javascript
复制
# 在python3中两个类定义没有任何区别,都是继承于object

# 在python2 中Myclass称为旧式类
class Myclass:
   pass

# 在python2 中Myclass1称为新式类
class Myclass1(object):
   pass
  • 在python2中
代码语言:javascript
复制
c = Myclass()
c1 = Myclass1()
print type(c)  # <type 'instance'>
print type(c1)  # <class '__main__.Myclass1'>
print type(Myclass)  # <type 'classobj'>
print type(Myclass1)  # <class 'type'>
  • 在python3中
代码语言:javascript
复制
c = Myclass()
c1 = Myclass1()
print(type(c))  # <class '__main__.Myclass'>
print(type(c1))  # <class '__main__.Myclass1'>
print(type(Myclass))  # <class 'type'>
print(type(Myclass1))  # <class 'type'>

使用type动态创建类

代码语言:javascript
复制
type(object_or_name, bases, dict)
type(object) -> the object's type
type(name, bases, dict) -> a new type

type(name, bases, dict)

  • name:类名
  • base:所继承的父类元祖
  • dict:字典(包含了属性和方法)

查看type的init方法

what也就是类名,必须要传入

代码语言:javascript
复制
def __init__(cls, what, bases=None, dict=None): # known special case of type.__init__
    """
    type(object_or_name, bases, dict)
    type(object) -> the object's type
    type(name, bases, dict) -> a new type
    # (copied from class doc)
    """
    pass

不传入bases和dict创建一个类

代码语言:javascript
复制
Myclass = type('Myclass')
print(Myclass)  # <class 'str'>
str1 = Myclass('124')
print(str1)  # 124
print(type(str1))  # <class 'str'>
print(type(Myclass))  # <class 'type'>

传入bases和dict创建一个类

代码语言:javascript
复制
def func(self):
    print(f'name:{self.name},age:{self.age}')


dict1 = {'name': 'zx', 'age': 18, 'func': func}
Myclass1 = type('Myclass1', (object,), dict1)
print(Myclass1)  # <class '__main__.Myclass1'>
print(type(Myclass1))  # <class 'type'>
m1 = Myclass1()
print(m1.name)  # zx
print(m1.age)  # 18
m1.func() # name:zx,age:18

对象引用

引用计数

代码语言:javascript
复制
a = 10 # a指向10,计数1
b = a # b引用a,a引用10,实际上b也引用10,计数2

引用计数减少

  • 对象的别名被显式销毁
  • 对象的一个别名被赋值给其他对象(a=10变为a=100,10的计数-1)
  • 对象从容器中移除,或容器销毁(对象从列表中移除,列表被销毁)
  • 一个引用离开了它的作用域(调用函数时,传入的参数,在函数结束的时候销毁)
代码语言:javascript
复制
import sys
print(sys.getrefcount(9999)) # 3
a = 9999
print(sys.getrefcount(9999)) # 4
b = a
print(sys.getrefcount(9999)) # 5

内置函数is和id

代码语言:javascript
复制
a = 10
b = 10
print(a is b)  # True
print(id(a), id(b))  # 4305020224 4305020224

li1 = [11, 22]
li2 = li1
li3 = [11, 22]
print(li1 is li2)  # True
print(li1 is li3)  # False
print(id(li1), id(li2), id(li3))  # 4326199176 4326199176 4326199240

深浅拷贝

  • 浅拷贝
代码语言:javascript
复制
li = [1, 2, 3]
li1 = li.copy()
li2 = li
# 4326199176 4326199240 4326199176
print(id(li), id(li1), id(li2))
li.append(33)
print(li, li1, li2)  # [1, 2, 3, 33] [1, 2, 3] [1, 2, 3, 33]

a = [1, 2]
li = [11, 22, a]
li1 = li.copy()
print(li, li1)  # [11, 22, [1, 2]] [11, 22, [1, 2]]
# 4348084936 4632599496
print(id(li), id(li1))  
a.append(3)
print(li, li1)  # [11, 22, [1, 2, 3]] [11, 22, [1, 2, 3]]
print(id(li), id(li1))  # 4354376392 4632599560
  • 深拷贝
代码语言:javascript
复制
li2 = copy.deepcopy(li)
# 4339696328 4632595208 4326199240
print(id(li), id(li1), id(li2))
# [11, 22, [1, 2, 3]] [11, 22, [1, 2, 3]] [11, 22, [1, 2, 3]]
print(li, li1, li2)
a.remove(3)
# 4339696328 4632595208 4326199240
print(id(li), id(li1), id(li2))
# [11, 22, [1, 2]] [11, 22, [1, 2]] [11, 22, [1, 2, 3]]
print(li, li1, li2)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试游记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 元类
    • 新式类 旧式类
      • 使用type动态创建类
      • 对象引用
        • 引用计数
          • 内置函数is和id
            • 深浅拷贝
            相关产品与服务
            容器服务
            腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档