python type用法_Python type()用法及代码示例

type()方法返回作为参数传递的参数(对象)的类类型。 type()函数主要用于调试目的。

可以将两种不同类型的参数传递给type()函数,即单参数和三参数。如果是单个参数type(obj)传递后,它返回给定对象的类型。如果三个参数type(name, bases, dict)传递后,它返回一个新的类型对象。

用法:

type(object)

type(name, bases, dict)

参数:

name:类的名称,该名称后来对应于该类的__name__属性。

bases:当前类派生的类的元组。以后对应于__bases__属性。

dict:包含类的名称空间的字典。以后对应于__dict__属性。

返回类型:

returns a new type class or essentially a metaclass.

代码1:

# Python3 simple code to explain

# the type() function

print(type([]) is list)

print(type([]) is not list)

print(type(()) is tuple)

print(type({}) is dict)

print(type({}) is not list)

输出:

True

False

True

True

True

代码2:

# Python3 code to explain

# the type() function

# Class of type dict

class DictType:

DictNumber = {1:'John', 2:'Wick',

3:'Barry', 4:'Allen'}

# Will print the object type

# of existing class

print(type(DictNumber))

# Class of type list

class ListType:

ListNumber = [1, 2, 3, 4, 5]

# Will print the object type

# of existing class

print(type(ListNumber))

# Class of type tuple

class TupleType:

TupleNumber = ('Geeks', 'for', 'geeks')

# Will print the object type

# of existing class

print(type(TupleNumber))

# Creating object of each class

d = DictType()

l = ListType()

t = TupleType()

输出:

代码3:

# Python3 code to explain

# the type() function

# Class of type dict

class DictType:

DictNumber = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'}

# Class of type list

class ListType:

ListNumber = [1, 2, 3, 4, 5]

# Creating object of each class

d = DictType()

l = ListType()

# Will print accordingly whether both

# the objects are of same type or not

if type(d) is not type(l):

print("Both class have different object type.")

else:

print("Same Object type")

输出:

Both class have different object type.

代码4:用于type(name, bases, dict)

# Python3 program to demonstrate

# type(name, bases, dict)

# New class(has no base) class with the

# dynamic class initialization of type()

new = type('New', (object, ),

dict(var1 ='GeeksforGeeks', b = 2009))

# Print type() which returns class 'type'

print(type(new))

print(vars(new))

# Base class, incorporated

# in our new class

class test:

a = "Geeksforgeeks"

b = 2009

# Dynamically initialize Newer class

# It will derive from the base class test

newer = type('Newer', (test, ),

dict(a ='Geeks', b = 2018))

print(type(newer))

print(vars(newer))

输出:

{'__module__':'__main__', 'var1':'GeeksforGeeks', '__weakref__':, 'b':2009, '__dict__':, '__doc__':None}

{'b':2018, '__doc__':None, '__module__':'__main__', 'a':'Geeks'}

应用范围:

type()函数本质上用于调试目的。如果将其他字符串函数(如.upper(),.lower(),.split())与从网络抓取工具中提取的文本一起使用,则可能无法使用,因为它们的类型可能不同,不支持字符串函数。结果,它将继续抛出错误,这些错误很难调试[将错误视为:GeneratorType没有属性lower()]。那时可以使用type()函数确定所提取文本的类型,然后在使用字符串函数或对其进行任何其他操作之前,将其更改为其他形式的字符串。

type()具有三个参数的参数可用于动态初始化类或具有属性的现有类。它还用于通过SQL注册数据库表。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值