Python论坛  - 讨论区

标题:[python-chinese] python-cookbook乱译(1) 验证一个东西是否是字符串

2006年01月04日 星期三 14:36

魏忠 weizhong2004 at gmail.com
Wed Jan 4 14:36:09 HKT 2006

Recipe 1.3. Testing Whether an Object Is String-like

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:
一个简单而快速的方法就是通过内建函数 isinstance 及 basstring检查这个东西是否是字符串或unicode对象。

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".
然而,这实在是一个糟透的方法。它完全忽视了python强大的多态行为。这种测试拒绝了unicode对象,用户自定义的str子类实例,以及其它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.
使用内建的 isinstance 函数,本文推荐的解决方案就好的多。 内建类型 basestring 刚好提供了这种途径。
basestring是str及unicode类型的基类,只要确认 isinstance 测试象预期的那样工作就可以了。basestring
是本质的空类型,就象对象一样,子类它没有什么花费。

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:

不幸运的是,标准的isinstance在检查从显式的UserString模块(python标准库)中的明显是string类型的UserString实例时却会失败和。这是由于,唉,这个类不是继承自basestring.
要检查这个类型,你得检查它是否符合一个字符串的行为,象下面这样:

def isStringLike(anobj):
    try: anobj + ''
    except: return False
    else: return True

This isStringLike function is slower and more complicated than the
isAStringfunction presented in the "Solution", but it does accept
instances of
UserString (and other string-like types) as well as instances of str and
unicode.
这个 isStringLike函数比我们刚刚提到的isAString函数慢,并且复杂,但它确实可以解决这个问题。

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:

众所周知,常规Python类型检查就象 鸭子分类:如果一个东西走起来象鸭子,并且会象鸭子那样嘎嘎叫,我们就完全可以把它当鸭子来处理了。这个
isStringLike函数就象检查是否会象鸭子一样嘎嘎叫的部分,这就够用了。若你需要检查一个对象的更多string-like特性,你可以在 try
子句中写下面这样一个表达式:

    try: anobj.lower( ) + anobj + ''

In my experience, however, the simple test shown in the
*isStringLike*function usually does what I need.
据我的经验,这个表达式所做的,isStringLike函数也做到了。

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).

绝大多数python式的类型校验方法(或其它校验任务)都是测试一下你要做的任务,检测并处理可能发后的错误和异常。
See Also

Documentation for the built-ins isinstance and basestring in the Library
Reference and Python in a Nutshell.
<5991535.html>

--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060104/f2f08976/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月05日 星期四 11:00

笨笨狗 chen.ruijie at gmail.com
Thu Jan 5 11:00:12 HKT 2006

这本书的第二版有大侠在译么?很是期待
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060105/181b551c/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月05日 星期四 11:15

魏忠 weizhong2004 at gmail.com
Thu Jan 5 11:15:28 HKT 2006

众人拾柴火焰高,俺刚接触python没几天,翻得很烂,希望有好事者一起做这事哦!

在06-1-5,笨笨狗 <chen.ruijie at gmail.com> 写道:
>
> 这本书的第二版有大侠在译么?很是期待
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060105/06799826/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月05日 星期四 12:03

Zoom Quiet zoom.quiet at gmail.com
Thu Jan 5 12:03:35 HKT 2006

在 06-1-5,魏忠<weizhong2004 at gmail.com> 写道:
> 众人拾柴火焰高,俺刚接触python没几天,翻得很烂,希望有好事者一起做这事哦!
>
> 在06-1-5,笨笨狗 <chen.ruijie at gmail.com> 写道:
> > 这本书的第二版有大侠在译么?很是期待
http://wiki.woodpecker.org.cn/moin/PyCookbook
汇总到Wiki 中吧,不然的话,很容易随邮件的淹没而遗失的

> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
>
> --
>
> 开飞机的舒克
> http://www.lvye.org/shuke
>  msn:weizhong at netease.com
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>


--
# Time is unimportant, only life important!
## 面朝开源,我心自由!

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月05日 星期四 12:10

魏忠 weizhong2004 at gmail.com
Thu Jan 5 12:10:52 HKT 2006

去wiki看了一下,那里在译的是这本书的第一版,第二版是2005年出的,包括了python2.4的新特性。
我还不太会用那个wiki,有时间好好研究一下。

2006/1/5, Zoom Quiet <zoom.quiet at gmail.com>:
>
> 在 06-1-5,魏忠<weizhong2004 at gmail.com> 写道:
> > 众人拾柴火焰高,俺刚接触python没几天,翻得很烂,希望有好事者一起做这事哦!
> >
> > 在06-1-5,笨笨狗 <chen.ruijie at gmail.com> 写道:
> > > 这本书的第二版有大侠在译么?很是期待
> http://wiki.woodpecker.org.cn/moin/PyCookbook
> 汇总到Wiki 中吧,不然的话,很容易随邮件的淹没而遗失的
>
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> >
> > --
> >
> > 开飞机的舒克
> > http://www.lvye.org/shuke
> >  msn:weizhong at netease.com
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> # Time is unimportant, only life important!
> ## 面朝开源,我心自由!
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060105/d3d2a990/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号