python type
1.type(name, bases, dict)
动态的创建一个类,等同于写class
,name
就是__name__
类的名称属性,base
就是__base__
基类,dict
就是__dict__
,类的属性和方法
A = type(name, bases, dict)
等价于
class A:__name__=__base__=__dict__=
例子1,下面的两种创建X类的方式是一致的:
class X:a = 1X = type('X', (object,), dict(a=1))
使用type
创建类,本质上和写class
没有区别
def say(self): # 定义一个函数,一会通过type放入类中print("hello")Person = type("Person",(object,),dict(say = say, name = "wang")) #使用 type() 函数创建类 另外注意,元组只有一个元素时`,`号不能省略
person = Person() # 实例化对象
person.say()#调用 say() 方法和 name 属性
print(person.name)
应用,在flask的config类中,使用此方法构造了配置类对象
def from_pyfile(self, filename, silent=False):"""Updates the values in the config from a Python file. This functionbehaves as if the file was imported as module with the:meth:`from_object` function.:param filename: the filename of the config. This can either be anabsolute filename or a filename relative to theroot path.:param silent: set to ``True`` if you want silent failure for missingfiles... versionadded:: 0.7`silent` parameter."""filename = os.path.join(self.root_path, filename) //构造文件路径d = types.ModuleType('config') //创建一个名字为'config'的模块类d.__file__ = filename # 模块指向的名字是刚才构造的文件路径try:with open(filename, mode='rb') as config_file: # 读取指定的文件exec(compile(config_file.read(), filename, 'exec'), d.__dict__) #先编译,然后执行,再赋值给d.__ditc__except IOError as e:if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR):return Falsee.strerror = 'Unable to load configuration file (%s)' % e.strerrorraiseself.from_object(d)return True
2.class type(name, bases, dict)
type除了做函数使用外,还可以作为类来使用,主要是和metaclass
相关
class Person(object,metaclass=type):def __init__(self, age): self.age = age
当我们在定义类的时候,默认省略了metaclass=type
具体详解可见https://blog.csdn.net/claroja/article/details/103254283
3.type(object)
返回一个对象的类型,等价于object.__class__
The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account.
type和isinstance
type不会认为子类是父类的类型,不会考虑继承关系。isinstance会任务子类是父类的类型,考虑继承关系。
type为对象的顶点,所有对象都创建自type。
object为类继承的顶点,所有类都继承自object。
python中万物皆对象,一个python对象可能拥有两个属性,__class__
和 __base__
,__class__
表示这个对象是谁创建的,__base__
表示一个类的父类是谁。
https://www.cnblogs.com/PrettyTom/p/6664808.html
http://c.biancheng.net/view/2292.html
https://www.cnblogs.com/mxuanli/p/9805122.html
https://docs.python.org/3/library/functions.html#type
python type相关推荐
- python type help copyright_Python关于import的实验(8)__init__.py文件内部代码的执行以及内部的导入和内部的变量...
Python官方文档参考链接: 常规包 Python 定义了两种类型的包,常规包 和 命名空间包. 常规包是传统的包类型,它们在 Python 3.2 及之前就已存在. 常规包通常以一个包含 __in ...
- Python Type Hints 从入门到实践
Python 想必大家都已经很熟悉了,甚至关于它有用或者无用的论点大家可能也已经看腻了.但是无论如何,它作为一个将加入高考科目的语言还是有它独到之处的,今天我们就再展开聊聊 Python. Pytho ...
- Python type 函数- Python零基础入门教程
目录 一.type 函数简介 type 函数语法 二.type 函数实战 三.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 一.t ...
- Python type函数和isinstance函数区别 - Python零基础入门教程
目录 一.Python type 函数简介 二.Python isinstance 函数简介 三.Python type 函数和 isinstance 函数区别 四.猜你喜欢 零基础 Python 学 ...
- Python Type Hint类型注解
原文地址:https://realpython.com/python-type-checking/ 在本指南中,你将了解Python类型检查.传统上,Python解释器以灵活但隐式的方式处理类型.Py ...
- [转载] python type() 判断数据类型
参考链接: Python type() type(a).__name__ == 'dict' :可判断a的类型是否类型为dict list tuple 这些也试用 栗子:
- python type函数_Python type()函数
python type函数 Python type()函数 (Python type() Function) Python has a lot of buit-in function. The typ ...
- python——type hints
python--type hints 介绍 type hints 主要是要指示函数的输入和输出的数据类型,数据类型在typing 包中,基本类型有str,list,dict等等. def hello( ...
- python type hint
python type hint 入门 初步使用 由于python是个动态类型语言,变量是什么类型是在变量运行的时候决定的,与他人合作或者代码躲多起来之后会变得难以阅读与调试,python3.5以上引 ...
- Python type hints 之 Optional,Union
1,前言 type hint 在pep484加入,我个人觉得这种类似于类型约束的(机制)有点违背了python简单.简洁的初衷,在慢慢向c# java 这种强类型语言看齐的节奏. 不过好在不强制使用, ...
最新文章
- Python OpenCV应用K均值聚类进行颜色量化
- html中#include file的使用方法
- Science:充满铵盐的环境依然发生固氮
- suse linux 安装oracle,SUSE Linux下安装Oracle 11g服务器
- Python学习进程
- Stay Hungry Stay Foolish——网络学习平台分享
- 发力企业级市场,微软Hololens开辟了一条VR新道路
- React面试题总结,一文说清!
- 手机MODEM 开发(14)----高通平台手机开发之Modem
- linux 优化 sysctl.conf,Linux内核sysctl.conf的优化设置
- Matlab修改显示数值格式/精度/小数位数
- Android的JNI【实战教程】2⃣️--AS下NDK环境配置及第一个工程
- [转]华人总结“十个”出了国才能知道秘密!
- 【历史上的今天】9 月 14 日:中国第一封电子邮件;世界上最早的电子银行系统;微软发布 Windows Me
- 一、Java11安装
- 零基础能学平涂插画么?
- 图像融合论文及代码网址整理总结(1)——多聚焦图像融合
- 利用requests库模拟访问博客来提升文章阅读量
- 家用计算机中的内存大约多少,64gb内存能装多少东西_64gb内存有多大-系统城
- 就同一个Service类中,一个事务方法调用另外一个有事务的方法
热门文章
- 五子棋python设计心得_python五子棋游戏的设计与实现
- python没有错误但是不显示结果_解决Pycharm无法显示matplotlib绘图问题
- 计算机网络基础+重点知识点
- Python在应用层实现UDP协议的可靠传输
- 《中学生可以这样学Python》84节微课免费观看地址
- 35岁学python爬虫_35岁码农的机器学习入门之路-python篇
- android如何让gps服务停止,android – 启动/停止GPS(或位置服务)时接收通知(通过BroadcastReceiver)...
- html元素中的click属性,从HTML中的onClick属性调用jQuery方法
- python加四位随机数_python生成四位随机数
- 转https_这个PDF转图片技巧,不用1分钟就能快速上手