首页 > 编程语言 >python3-type

python3-type

时间:2023-04-17 10:00:51浏览次数:40  
标签:name object 类型 dict print type python3

1、介绍

type本身是内建模块中的一个类,其主要作用是用于判断变量的数据类型。

2、类

class type(object):
    def __init__(cls, what, bases=None, dict=None):
        """
        type(object_or_name, bases, dict)
        type(object) -> the object's type
        type(name, bases, dict) -> a new type

        """
        pass
  • 一个参数,作用是判断变量的数据类型
  • 三个参数,基于此创建指定类型的变量
    • name,str类型,新创建对象的类型名
    • bases,tuple类型,name指向类的父类,如果没有的话写入object即可
    • dict,dict类型,name指向的类初始化需要的参数
    • 返回即为创建的对象类型

3、示例

(1)获取类型

print(type(type("")))  # <class 'type'>
print(type("123"))  # <class 'str'>
print(type(123))  # <class 'int'>

(2)判断类型

if type("123") == str:  # 判断类型
    print("相同")

(3)创建对象

class stu:
    def __init__(self, name):
        self.name = name


z = type("stu", (object,), {"name": "abc"})
print(z)  # <class '__main__.stu'>
print(z.name)  # abc

标签:name,object,类型,dict,print,type,python3
From: https://www.cnblogs.com/wd404/p/17324879.html

相关文章

  • python3-len
    1、介绍python3中,可以使用函数len获取容器对象的长度,比如str字符串、list列表等。2、函数deflen(*args,**kwargs):pass返回类型为int,如果长度为0,则就返回0不能对非容器变量使用,会报错,比如int、None值3、示例#不能对非容器变量使用,会报错#print(len(123))pri......
  • Could not autowire. No beans of BookDao' type found
    在做Spring或者SpringBoot项目时,在测试类中创建一个实体类属性并进行自动装配时,回报红:Couldnotautowire.NobeansofBookDao'typefound(只是环境的原因,不是错误) 直接Alt+Enter,将错误改成警告即可 ......
  • HTML的enctype属性
    先了解一下form表单提交的过程:(1)先把form表单里的表单元素的name属性和value属性进行收集(2)按照enctype属性的设置,选择合适的编码方式,对数据进行编码,放在请求头里(3)浏览器进行发送enctype有三种:(1)application/x-www-form-urlencoded:在发送前编码所有字符,表单里的数据被编码成名称......
  • 部署Python3
    1、安装编译工具yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgdbm-develdb4-devellibpcap-develxz-develyuminstalllibffi-devel2、下载软件包并解压wgethttps://ww......
  • TypeScript type 关键字和 interface 关键字
    前言type和interface都可以声明TS类型。typePoint1={x:number;y:number;};interfacePoint2{x:number;y:number;};它们两个声明这个对象类型有任何区别吗?很明显没有,我认为最能区分它们两个的标志是,type有一个=赋值等号。typetype可以做类......
  • Python3基本请求库-urllib
    urlliburlopen一个基本请求fromurllibimportrequest,parsedefApi():#禁用证书验证ssl._create_default_https_context=ssl._create_unverified_contextresponse=request.urlopen('https://www.baidu.com/')print(response.read().decode(�......
  • vue3+echart5 遇到的坑 Cannot read properties of undefined (reading 'type')(转)
    原文:https://www.cnblogs.com/Bin-x/p/15342949.html1、错误说明vue3中,使用data的方式初始化echart图表exportdefault{data(){return{chart:null,...}},mounted(){this.chart=echarts.init(document.getElementById(this.id))......
  • python3正则-表达式
    1、介绍这里整理了表达式,即pattern参数的语法。其本质是一个str类型。2、开始和结尾(1)^^匹配字符串的开头(2)$$匹配字符串的末尾(3)整体匹配同时使用^和$3、匹配次数(1)贪婪匹配和非贪婪匹配模式贪婪匹配,在允许的范围内尽可能多的匹配表达式次数。比如*、+、{n,m}等......
  • python3正则-修饰符flags
    1、介绍在re模块的函数中,多数允许设置修饰符flags参数,其用于对匹配做优化和个性设置。2、修饰符修饰符实质上是int参数类型,可以直接指定int类型,也可以通过re调用变量名称指定。修饰符描述re.I使匹配对大小写不敏感re.L做本地化识别(locale-aware)匹配re.M多行......
  • python3正则-Match类
    1、介绍re.match、re.fullmatch和re.search这三个函数,如果存在匹配,其返回pattern初次匹配的结果,类型就为re.Match。2、类和初始化3、方法pattern="(ab)c"result=re.match(pattern=pattern,string="abcdef")print(result)(1)start获取pattern整体匹配的开始索引,返回类......