Python中type的用法
描述
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的用法的更多相关文章
- python中argparse模块用法实例详解
python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...
- Python中super的用法【转载】
Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143 收藏 展开 转载自 Python面向对象中super用法与MRO ...
- python 中del 的用法
python中的del用法比较特殊,新手学习往往产生误解,弄清del的用法,可以帮助深入理解python的内存方面的问题. python的del不同于C的free和C++的delete. 由于pyth ...
- Python中type与Object的区别
Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...
- 【313】python 中 print 函数用法总结
参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...
- python中MySQLdb模块用法实例
篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...
- python中hashlib模块用法示例
python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...
- Python Deque 模块使用详解,python中yield的用法详解
Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...
- Python中With的用法
在看Dive Into Python中有关描述文件读写那章节的时候,看到了有关with的用法,查阅下相关资料,记录下来,以备后用. 官方的reference上有关with statement是这样说的 ...
随机推荐
- 【BZOJ3691】游行 费用流
[BZOJ3691]游行 Description 每年春季,在某岛屿上都会举行游行活动.在这个岛屿上有N个城市,M条连接着城市的有向道路.你要安排英雄们的巡游.英雄从城市si出发,经过若干个城市,到城 ...
- Unity3D笔记 切水果二 刀光剑影
一.步骤一创建一个空GameObject.js 二.代码 #pragma strict var myColor:Color; var firstPosition:Vector3;//鼠标点击的第一个点 ...
- Thinkphp框架下对某个字段查询数据的时候进行唯一过滤,返回唯一不同的值
方法一. DISTINCT 方法用于返回唯一不同的值 . *distinct方法的参数是一个布尔值. 用法: $data = $Model->Distinct(true)->field(' ...
- github中删除一个repository
(1) 首先进入相应的repository,然后点击setting 2,点击 delete the repository (3) 输入要删除的repository名字,即可删除
- Linux下搭建Hadoop集群(Centos7.0)
Hadoop集群安装 概述 集群 cluster,将很多任务进程分布到多台计算机上:通过联合使用多台计算机的存储.计算能力完成更庞大的任务.为了实现无限量的存储和计算能力,在生产环境中必须使用集群来满 ...
- Docker 学习应用篇三:使用docker搭建的环境,安装thinksns
Thinksns 是一个开源的社交网站,因为目前的项目需要用到这个,所以就下载一个安装试试看. 之前在windows上用了wamp,安装的很顺利.但是项目是要部署到linux上的.于是我便用了Dock ...
- PAT甲1031 Hello World for U【字符串】
1031 Hello World for U (20 分) Given any string of N (≥5) characters, you are asked to form the chara ...
- python MD5步骤
https://www.cnblogs.com/zipon/p/8340720.html import hashlib def get_token(): md5str = "abc" ...
- idea 创建的maven+spring+mybatis项目整合 报错无法创建bean
报错如下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with n ...
- 启动InnoDB引擎的方法
启动InnoDB引擎的方法 http://down.chinaz.com/server/201207/2090_1.htm 启动InnoDB引擎的方法 Mysql中默认的是MyISAM数据引擎,可惜此 ...