免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2953 | 回复: 0
打印 上一主题 下一主题

【抄录】Testing Whether an Object Is String-like [复制链接]

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-09 20:24 |只看该作者 |倒序浏览

摘自 Python Cookbook
Credit: Luther Blissett
Problem
You need to test if an object, typically an argument to a
function or method you're writing, is a string (or more precisely, whether the
object is string-like).
Solution
A simple and fast way
to check whether something is a string or Unicode object is to use the built-ins
isinstance and basestring, as follows:
def isAString(anobj):
    return isinstance(anobj, basestring)
Discussion
The first approach to solving this recipe's problem that comes
to many programmers' minds is type-testing:
def isExactlyAString(anobj):
    return type(anobj) is type('')
However, this approach is pretty bad, as it willfully destroys
one of Python's greatest strengthssmooth, signature-based polymorphism. This
kind of test would reject Unicode objects, instances of user-coded subclasses of
str, and instances of any user-coded type that is meant to be
"string-like".
Using the isinstance built-in function, as recommended
in this recipe's Solution, is much better. The built-in type basestring
exists exactly to enable this approach. basestring is a common base
class for the str and unicode types, and any string-like type
that user code might define should also subclass basestring, just to
make sure that such isinstance testing works as intended.
basestring is essentially an "empty" type, just like object,
so no cost is involved in subclassing it.
Unfortunately, the canonical isinstance checking fails
to accept such clearly string-like objects as instances of the
UserString class from Python Standard Library module
UserString, since that class, alas, does not inherit from basestring. If you need to
support such types, you can check directly whether an object behaves like a
stringfor example:
def isStringLike(anobj):
    try:
        anobj + ''
    except:
        return False
    else:
        return True
This isStringLike function is slower and more
complicated than the isAString function presented in the "Solution",
but it does accept instances of UserString (and other string-like
types) as well as instances of str and unicode.
The general Python approach to
type-checking is known as duck typing: if it walks like a duck and quacks
like a duck, it's duck-like enough for our purposes. The isStringLike
function in this recipe goes only as far as the quacks-like part, but that may
be enough. If and when you need to check for more string-like features of the
object anobj, it's easy to test a few more properties by using a richer
expression in the TRy clausefor example, changing the clause to:
    try: anobj.lower( ) + anobj + ''
In my experience, however, the simple test shown in the
isStringLike function usually does what I need.
The most Pythonic approach to type
validation (or any validation task, really) is just to try to perform whatever
task you need to do, detecting and handling any errors or exceptions that might
result if the situation is somehow invalidan approach known as "it's easier to
ask forgiveness than permission" (EAFP). try/except is the key
tool in enabling the EAFP style. Sometimes, as in this recipe, you may choose
some simple task, such as concatenation to the empty string, as a stand-in for a
much richer set of properties (such as, all the wealth of operations and methods
that string objects make available).
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/95893/showart_2066513.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP