English 中文(简体)
书写只采用某种特定类型方法的适当时机是什么?
原标题:What is the proper python way to write methods that only take a particular type?
  • 时间:2010-11-14 21:38:45
  •  标签:
  • python

我有一项职能,即在必要时进行扼杀,把事情交给它,并将结果退回。

我的自然倾向是,仅仅回报结果,因为结果涉及扼杀,如果它失败,则让打电话者感到例外。 然而,这一职能具有缺省价值,我刚刚回过来。

我的问题是:如果有人对这种方法寄出一些意想不到的东西,又回到用户所期望的东西? 这种方法应当失败,但如何执行?

最佳回答

没有必要这样做,但如果你知道该物体属于你无法处理的类别,那么如果你想要你能够用上的方法。 这样做的一个原因是帮助人们理解为什么呼吁方法失败,给他们一些帮助加以固定,而不是给他们留下你职能内部的模糊错误。

标准图书馆的一些方法就是:

>>> [] + 1
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate list (not "int") to list
问题回答

You can use decorators for this kind of thing, you can see an example here.

但是,迫使参数属于某种特定类型,是 t。

粉碎的假设是,我们都是智慧的成年人,读到文件。 如果你仍然想这样做,那么你就不应该坚持实际的类型,而是在论据不支持你所需要的行动时,就只能满足例外情形。

def foo(arg):
    try:
        return arg + "asdf"
    except TypeError:
        return arg

违约价值如何? 如果打电话者没有穿透镜,你是否希望回去虚设的价值? 在这种情况下:

def yourFunc( foo ):
    try:
        return foo + " some stuff"
    except TypeError:
        return "default stuff"

1. 空间-C0wb0y如果希望恢复动力而不作扼杀,则有正确的答案,还有试图将某些东西改成扼子的选择:

def yourFunc2( bar ):
    return str(bar) + " some stuff"

这些工作将多种多样。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签