描述

python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型。当有三个参数的时候返回一个类对象。

语法

type(object)

type(name, bases, dict)

用法

一个参数

type(object)

返回一个对象的类型,如:

In [1]: a = 10 

In [2]: type(a)
Out[2]: int

三个参数

tpye(name, bases, dict)

  • name 类名
  • bases 父类的元组
  • dict 类的属性方法和值组成的键值对

返回一个类对象:

# 实例方法
def instancetest(self):
print("this is a instance method") # 类方法
@classmethod
def classtest(cls):
print("this is a class method") # 静态方法
@staticmethod
def statictest():
print("this is a static method") # 创建类
test_property = {"name": "tom", "instancetest": instancetest, "classtest": classtest, "statictest": statictest}
Test = type("Test", (), test_property) # 创建对象
test = Test()
# 调用方法
print(test.name)
test.instancetest()
test.classtest()
test.statictest()

运行结果:

tom
this is a instance method
this is a class method
this is a static method

使用help打印Test的详细信息:

class Test(builtins.object)
| Methods defined here:
|
| instancetest(self)
| # 实例方法
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| classtest() from builtins.type
| # 类方法
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| statictest()
| # 静态方法
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| name = 'tom'

可以看出我们创建了一个Test类,包含一个实例方法statictest,类方法classtest,静态方法statictest,和一个属性name = 'tom'。

type和isinstance

type不会认为子类是父类的类型,不会考虑继承关系。isinstance会任务子类是父类的类型,考虑继承关系。

Type和Object

type为对象的顶点,所有对象都创建自type。

object为类继承的顶点,所有类都继承自object。

python中万物皆对象,一个python对象可能拥有两个属性,__class____base____class__ 表示这个对象是谁创建的,__base__ 表示一个类的父类是谁。

In [1]: object.__class__
Out[1]: type In [2]: type.__base__
Out[2]: object

可以得出结论:

  • type类继承自object
  • object的对象创建自type

Python中type的用法的更多相关文章

  1. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

  2. Python中super的用法【转载】

    Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143  收藏 展开 转载自 Python面向对象中super用法与MRO ...

  3. python 中del 的用法

    python中的del用法比较特殊,新手学习往往产生误解,弄清del的用法,可以帮助深入理解python的内存方面的问题. python的del不同于C的free和C++的delete. 由于pyth ...

  4. Python中type与Object的区别

    Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...

  5. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  6. python中MySQLdb模块用法实例

    篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...

  7. python中hashlib模块用法示例

    python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...

  8. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

  9. Python中With的用法

    在看Dive Into Python中有关描述文件读写那章节的时候,看到了有关with的用法,查阅下相关资料,记录下来,以备后用. 官方的reference上有关with statement是这样说的 ...

随机推荐

  1. 【笔记】javascript权威指南-第六章-对象

    对象 //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.28 对象的定义: 1.对象是一种复合值:将很多值(原始值或者对象)聚合在一起,可以通过名字访问这些值. ...

  2. iOS - UIScreen的 bound、frame、scale属性

    A UIScreen object contains the bounding rectangle of the device’s entire screen. When setting up you ...

  3. Spark2 Dataset持久化存储级别StorageLevel

    import org.apache.spark.storage.StorageLevel // 数据持久缓存到内存中//data.cache()data.persist() // 设置缓存级别data ...

  4. vue之用法

    一.安装 对于新手来说,强烈建议大家使用<script>引入 二. 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. 三.使用V ...

  5. Scala学习笔记(1)-基本类型归纳

    1.小试牛刀 使用Scala自带的REPL shell(Read Evaluate Print Loop)学习和尝试Scala语言库,创建的变量在会话期间都是有效的. Ctrl+D可退出REPL sh ...

  6. Python模块NumPy中的tile(A,rep) 函数

    from NumPy import * 函数形式: tile(A,rep) 功能:重复A的各个维度 参数类型: - A: Array类的都可以 - rep:A沿着各个维度重复的次数 这个英文单词的本意 ...

  7. codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]

    题目链接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man with a pair o ...

  8. 冒泡排序之python

    冒泡排序(Bubble sort) 两两比较相邻记录的关键字,如果反序则交换,直到没有反序记录为止. 1.算法描述: 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工 ...

  9. referrer privacy hotlinking

    https://en.wikipedia.org/wiki/HTTP_referer https://zh.wikipedia.org/wiki/HTTP参照位址 inline linking, of ...

  10. 浏览器指纹 - HTTP cookie 浏览器指纹 欺诈检测 浏览器id hash 浏览器插件信息 canvas 字体信息

    详解浏览器cookie和浏览隐私之间的关系http://www.iefans.net/cookie-yinsi-guanxi/ 详解浏览器cookie和浏览隐私之间的关系 浏览器相关 互联网 2013 ...