如何在函数中返回“not string”错误? [英] How to return an "not string' error in function?

查看:150
本文介绍了如何在函数中返回“not string”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我必须声称我是菜鸟所以请帮助我不要

怪我:)


例如:


def test(s):

如果类型!=? :

返回

#所以我想建立一个情况,如果< sis not string

#then< return>,但是应该如何写< ??

#Or还有其他办法吗?


任何建议都会赞赏


和平

解决方案

def test(s):


if type(s)!=? :

返回

#所以我想建立一个情况,如果< sis not string

#then< return>,但是怎么写#< ??

#Or还有其他办法吗?


>> isinstance(" hello",basestring)



True


>> isinstance(uhello",basestring)



True


对于常规字符串和unicode

字符串,这将返回true。如果这是一个问题,你可以使用


>> import types
isinstance(" hello",types.StringType)



True


>> isinstance(u" hello",types.StringType)



False


>> isinstance(" hello",types.UnicodeType)



False


>> isinstance(uhello,types.UnicodeType)



真实


....或者,如果你不想用类型来限定它们。每次,

你可以使用


>>从类型导入StringType,UnicodeType



将它们带入本地命名空间。


HTH,


-tkc


非常感谢它完美地回答了我的谦虚问题: )


Tim Chase< py ********* @ tim.thechases.comwrote:


对于常规字符串和unicode

字符串,这将返回true。如果这是一个问题,你可以使用


> import types
isinstance(" hello",types .StringType)



True


> isinstance(u" hello",types.StringType)



False


> isinstance(" hello",types.UnicodeType)



False


> isinstance(uhello,types.UnicodeType)



True


......或者,如果你不想用类型来限定它们。每次,

你可以使用


> from types import StringType,UnicodeType



将它们带入本地命名空间。



它们已分别位于内置命名空间中,分别是更常用的名称

str和unicode,因此无需导入它们,只需使用

他们:


>> isinstance (" hello",str)



True


....等......


first of all I have to claim that I''m a noob so please help me don''t
blame me:)

for example:

def test(s):
if type(s) != ? :
return
#So here I want establish a situation about that if <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?

Any suggestion would be appreciate

Peace

解决方案

def test(s):

if type(s) != ? :
return
#So here I want establish a situation about that if <sis not string
#then <return>, but how should write the <??
#Or is there any other way to do it?

>>isinstance("hello", basestring)

True

>>isinstance(u"hello", basestring)

True

This will return true for both regular strings and for unicode
strings. If that''s a problem, you can use

>>import types
isinstance("hello", types.StringType)

True

>>isinstance(u"hello", types.StringType)

False

>>isinstance("hello", types.UnicodeType)

False

>>isinstance(u"hello", types.UnicodeType)

True

....or, if you don''t want to qualify them with "types." each time,
you can use

>>from types import StringType, UnicodeType

to bring them into the local namespace.

HTH,

-tkc



Thank you so much it answers my humble question perfectly:)


Tim Chase <py*********@tim.thechases.comwrote:

This will return true for both regular strings and for unicode
strings. If that''s a problem, you can use

>import types
isinstance("hello", types.StringType)

True

>isinstance(u"hello", types.StringType)

False

>isinstance("hello", types.UnicodeType)

False

>isinstance(u"hello", types.UnicodeType)

True

...or, if you don''t want to qualify them with "types." each time,
you can use

>from types import StringType, UnicodeType


to bring them into the local namespace.

They already are in the builtin namespace under their more usual names of
str and unicode respectively, so there is no need to import them, just use
them:

>>isinstance("hello", str)

True

.... etc ...


这篇关于如何在函数中返回“not string”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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