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的用法的更多相关文章
- day01-day04总结- Python 数据类型及其用法
Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...
- python函数的用法
python函数的用法 目录: 1.定义.使用函数 1.函数定义:def 2.函数调用:例:myprint() 3.函数可以当作一个值赋值给一个变量 例:a=myprint() a() 4.写r ...
- python之函数用法isinstance()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isinstance() #isinstance() #说明:返回一个布尔值,判断数据 ...
- 用Python 的一些用法与 JS 进行类比,看有什么相似?
Python 是一门运用很广泛的语言,自动化脚本.爬虫,甚至在深度学习领域也都有 Python 的身影.作为一名前端开发者,也了解 ES6 中的很多特性借鉴自 Python (比如默认参数.解构赋值. ...
- Python中flatten用法
Python中flatten用法 原创 2014年04月16日 10:20:02 标签: Python / flatten 22667 一.用在数组 >>> a = [[1,3],[ ...
- Python高阶用法总结
目录 1. lambda匿名函数 1.1 函数式编程 1.2 应用在闭包 2. 列表解析式 3. enumerate内建函数 4. 迭代器与生成器 4.1 迭代器 4.3 生成器 5. 装饰器 前言: ...
- python map 的用法
map的用法 ——.我们来分析map在python的源码 class map(object): """ map(func, *iterables) --> map ...
- Python回调函数用法实例
Python回调函数用法实例 作者:no.body链接:https://www.zhihu.com/question/19801131/answer/27459821 什么是回调函数? 我们绕点远路来 ...
- python操作redis用法详解
python操作redis用法详解 转载地址 1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用 ...
随机推荐
- 关于 Data URI Scheme -- data:image/jpg;base64
转载一篇大神的文章 大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqA ...
- Linux 磁盘空间大小统计du命令常见使用方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hongweigg/article/details/37692057 在 Linux下,能够对某个文件 ...
- HIVE简单操作
1.hive命令登录HIVE数据库后,执行show databases;命令可以看到hive数据库中有一个默认的default数据库. [root@hadoop hive]# hive Logging ...
- 安装与破解PyCharm专业版
安装与破解PyCharm专业版(有两种方法) 一.安装与破解PyCharm专业版 1.安装pycharm(建议安装python3) 这是网址:(https://www.jetbrains.com/py ...
- testetest
resumeLoad renren静态 foolday \ swImg activity01
- spring boot 启动类一定要放置到包的根目录下,也就是和所有包含java文件的包在同一级目录。如果不放置在根目录下,将会提示 no mybatis mapper was found
spring boot 启动类一定要放置到包的根目录下,也就是和所有包含java文件的包在同一级目录.将会将同一目录下的包扫描成bean. 如果不放置在根目录下,将会提示 no mybatis map ...
- Wix制作安装包
Wix制作安装包,找起资料来很费劲,记录一下: Product.wxs,该文件只能制作出msi形式的安装包,不能做到自动检测framework. <?xml version="1.0& ...
- Centos7系统防火墙上开端口
//permanent 永久生效 没有此参数重启失效 firewall -cmd --zone=public --add -port=80/tcp --permanent //开 ...
- Util.FSUtils: Waiting for dfs to exit safe mode
有好几次,启动Hadoop和HBase之后,执行jps命令,已经看到有HMaster的进程, 但是进入到HBase的shell,执行一个命令,会出现下面的错误: ERROR: org.apache.h ...
- numpy.newaxis()
np.newaxis 功能:为numpy.ndarray(多维数组)增加一个轴 np.newaxis 在使用和功能上等价于 None,查看源码发现:newaxis = None,其实就是 None 的 ...