简单对象的类型 [英] type of simple object

查看:52
本文介绍了简单对象的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我怎么知道简单对象的类型是元组还是列表或整数,对于

例子我的函数应该理解什么是对象类型传递

其论点


Pujo

解决方案

使用类型函数!

s =''test''
i = 25
type(s)
< type''str''> type(i)



< type''int''>


" AJ **** @ gmail.com" < AJ **** @ gmail.com>写道:

我怎么知道简单对象的类型是元组或列表或整数,对于
例子我的函数应该理解传递的对象类型是什么
它的论点



以降低的Pythonicity程度排序的答案:


1)你错了,函数几乎肯定不应该关心

对象是元组还是列表(或整数)[1]。


2)isinstance(obj,list)等


3)类型(obj)


[1]您可能想检查对象是否能够执行

无论是什么你希望列表或元组为你做的。对于

示例:

def total(对象):

尝试:

返回总和(对象)#The list-or-tuple case
除了TypeError之外


返回对象#整数个案


结果< type''str''>

如何检查它,因为它不是正确的字符串?


Pujo


Hi,

How do I know type of simple object is tuple or list or integer, for
example my function should understand what is the object type passed in
its argument

Pujo

解决方案

use the type function!

s = ''test''
i = 25
type(s) <type ''str''> type(i)


<type ''int''>


"aj****@gmail.com" <aj****@gmail.com> writes:

How do I know type of simple object is tuple or list or integer, for
example my function should understand what is the object type passed in
its argument


Answers ordered in decreasing degree of Pythonicity:

1) You are mistaken, the function almost certainly should not care
whether the object is a tuple or a list (or integer)[1].

2) isinstance(obj, list) etc.

3) type(obj)

[1] You probably want to check whether the object is capable of doing
whatever it is that you expect lists or tuples to do for you. For
example:
def total(object):
try:
return sum(object) # The list-or-tuple case
except TypeError:
return object # The integer case


The result <type ''str''>
How can I check it since it is not a string right?

Pujo


这篇关于简单对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆