对于type,经常会用到的是判断类型,但是判断类型更推荐的一种方式是使用isinstance();但是很少会用到type的另外一个功能,生成一个新的类型,看官方解释:

class type(name, bases, dict)

With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the name attribute; the bases tuple itemizes the base classes and becomes the bases attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the dict attribute. For example, the following two statements create identical type objects:

>>> class X(object):

... a = 1

...

>>> X = type("X", (object,), dict(a=1))

这样就可以产生一个新的类型X。

再举个demo:

django框架中的BaseManager

@classmethod

def from_queryset(cls, queryset_class, class_name=None):

if class_name is None:

class_name = "%sFrom%s" % (cls.__name__, queryset_class.__name__)

class_dict = {

"_queryset_class": queryset_class,

}

class_dict.update(cls._get_queryset_methods(queryset_class))

return type(class_name, (cls,), class_dict)

over...

python中的type函数-python的type函数相关推荐

  1. 成功解决Python中出现的TypeError: object of type 'zip' has no len()

    成功解决Python中出现的TypeError: object of type 'zip' has no len() 不罗嗦,直接解决问题! 目录 解决问题 解决思路 解决方法 解决问题 TypeEr ...

  2. python 中的static-method (静态函数), classmethod(类函数 ), 成员函数

    python 中的static-method (静态函数), classmethod(类函数 ), 成员函数 简介 类似Java.CPP中的类,可以定义static method.class meth ...

  3. python变量必须以字母和下划线_【转载】关于python中带下划线的变量和函数的意义...

    Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读, ...

  4. python魔法方法与函数_在Python中画图(基于Jupyter notebook的魔法函数)

    这篇文章主要介绍了在Python中画图(基于Jupyter notebook的魔法函数),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先展示一段相 ...

  5. python的输入函数是什么意思_在Python中,用于获取用户输入的函数是

    在Python中,用于获取用户输入的函数是 Whichofthefollowingmodernfarmtoolsaredevelopedbasedonpushsickle?A:Reaper.B:Gra ...

  6. 在python中用于获取用户输入的是-在Python中,用于获取用户输入的函数是

    在Python中,用于获取用户输入的函数是 Whichofthefollowingmodernfarmtoolsaredevelopedbasedonpushsickle?A:Reaper.B:Gra ...

  7. python画抛物线_在python中利用最小二乘拟合二次抛物线函数的方法

    1.最小二乘也可以拟合二次函数 我们都知道用最小二乘拟合线性函数没有问题,那么能不能拟合二次函数甚至更高次的函数呢?答案当然是可以的.下面我们就来试试用最小二乘来拟合抛物线形状的的图像. 对于二次函数 ...

  8. Python中通过索引名称提取数据loc()函数Python中通过行和列下标提取数据iloc()函数

    [小白从小学Python.C.Java] [Python全国计算机等级考试] [Python数据分析考试必会题] ● 标题与摘要 Python中通过索引名称提取数据 loc()函数 Python中通过 ...

  9. python 中os.path 的一些路径常用函数

    python 中os.path 的一些路径常用函数 import os path = 'AAA/BBB/ccc.txt' aa=os.path.dirname(path) #返回文件路径 print( ...

  10. python中用于获取用户输入的函数是_在Python中,用于获取用户输入的函数是.

    在Python中,用于获取用户输入的函数是. 答:input() 关于新生儿生理性体重下降的恢复时间,下列正确的是出生后 答:7~10天 中国大学MOOC: 未来城市只有同时具备下列哪几个组成部分,才 ...

最新文章

  1. OPENCV已知内参求外参
  2. 搜狗皮肤php怎么转换,如何给搜狗输入法更换皮肤和输入方式
  3. 聊一聊SpringCloudNetflix的五大组件(神兽)
  4. linux umount 时出现device is busy 的处理方法--fuser
  5. BGD-py实现学习【1】[转载]
  6. 用后端接口调试工具Insomnia发送请求
  7. 2004-7-21/22+ DataGrid知识整理
  8. VCC,GND,VSS,VDD的理解
  9. 【IP SSL】内网IP SSL证书Nginx部署
  10. 仿京东的注册页面, 使用jQuery进行表单验证
  11. SpringBoot Actuator未授权访问漏洞修复
  12. 年薪60w的程序员与年薪6w的极品程序员,差距怎么这么大呢?
  13. 背篼酥课堂第九课--前端知识、APP知识
  14. 迪士尼挖角波士顿动力,耗时3年打造漫威英雄机器人,1:1复刻效果堪比CG
  15. WEB介绍(beego)
  16. Vue(总结完毕)2021.1.1 新年快乐
  17. Linux虚拟主机安装WordPress教程
  18. 微软算法面试题(2021最新)
  19. 计算机联结两个硬盘,台式电脑怎么连接两块硬盘
  20. linux中的孤儿进程

热门文章

  1. TOMCAT 连接池数据库密码加密方法
  2. Uncaught ReferenceError: FileAsyncWriter is not defined
  3. 《天天数学》连载37:二月六日
  4. Python学习笔记:过滤N位数并绘制折线图
  5. 大数据学习笔记14:MR案例——招聘数据分析
  6. Python数据分析学习笔记02:Python语言基础、IPython与Jupyter笔记本
  7. 【英语学习】【English L06】U08 News L6 News like to read
  8. 【英语学习】【WOTD】prodigous 释义/词源/示例
  9. 【英语学习】【WOTD】nomothetic 释义/词源/示例
  10. 安卓一步一步搭建组件化