python3中怎么判断传入的参数是否是unicode

查看:2167
本文介绍了python3中怎么判断传入的参数是否是unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

网上的教程都说使用isinstance( XXX , unicode)来判断
但是我在python3下这么使用会报name 'unicode' is not defined的错误,想看一下isinstance()是怎么实现的,传入参数的说明,结果发现isinstance的定义是下面这个样子,完全看不出想要判断unicode该传入什么参数,顺带问一下为什么python好多自带的参数都是这么定义的,def XXX(): return none,没有功能实现的代码

def isinstance(object, class_or_type_or_tuple):
  """ isinstance(object, class-or-type-or-tuple) -> bool
  
  Return whether an object is an instance of a class or of a subclass thereof.
  With a type as second argument, return whether that is the object's type.
  The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
  isinstance(x, A) or isinstance(x, B) or ... (etc.). """
  return None

回到正题,python3下该怎么判断是否unicode呢?

解决方案

Python 3中基本的str就是unicode,所以可以直接判断str:

>>> isinstance('s', str)
True

这篇关于python3中怎么判断传入的参数是否是unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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