English 中文(简体)
甲型六氯环己烷——产生选择性母体的模式?
原标题:Python API - pattern for creating optional parenthesis?

我正在设计一个斯堪的图书馆的APICS,并陷入这样一种局面,即我认为我的想象力可能超越了斯图尔的丰富能力。

(I want to apologize to any Pythonistas who may get offended by the Ruby-ness of my design)

Basically, there is a class with a few methods on it, each of which returns self, so that they can be chained together. So:

>>> a = MyKlass()
>>> b = a.method_one()
>>> c = b.method_two()

将与

>>> a = MyKlass()
>>> c = a.method_one().method_two()

我的问题是,能否使母心选择。 我已经学到了使用<代码>__getattr__和__bet__>以操纵可移动物体的比照,但已无法充分执行。 现在,我有<代码>__getattr__。 核对属性,然后确定<代码> 姓名/代码”是一种方法或属性(或更准确地说,它是否可以使用)。

我的问题是,如果是可呼吁的,我需要__getattr__来做这样的事情:

>>> class MyKlass(object):
>>>     def __getattr__(self, name):
>>>         # pseudocode, you get the idea...
>>>         if name is callable:
>>>             def callme(*args, **kwds):
>>>                 # do stuff
>>>             return callme
>>>         else:
>>>             # just return the attribute

因此,它归还了一个可提的物体。 然而,这意味着,我 could不出使母体可以选择,因为可呼吁的物体将归还,但从未被称作:

>>> a = MyKlass()
>>> c = a.method_one.method_two    # AttributeError, since a bound function object does not have attribute "method_two"

我认为这样做的唯一途径是能够在守则中“展望未来”,并确定“<>代码><>>/代码>是否随后是括号。 然后,如果<代码> 姓名/代码>是可称呼的,但又无人称呼,我可以不提出理由而退回<代码> 姓名/代码>的结果。

我认为,这或许是站不住脚的,但我认为,我会问一看问题。

最佳回答

我认为,你想这样做是nut的。 你有什么理由? 提出论点的方法是什么? (见下文)

如果你使用<代码>__getattribute__,而不是getattr__,则似乎不作论证。

工作实例:

class Test(object):
    def __getattribute__(self, name):
        attr = super(Test, self).__getattribute__(name)
        if callable(attr):
            return attr()
        return attr
    def f(self):
        print  f 
        return self
    def g(self):
        print  g 
        return self
    def h(self, x):
        print x
        return self

t = Test()
t.f.g # Works fine
t.f().g # Fails - the parens aren t optional, they must be left off now
t.f.g.h(1) # Fails - unable to provide an argument

为了处理这一论点,请could检查有关职能,如果它不提出任何论点(不说自理),则只叫它。 见:rel=“nofollow” http://docs.python.org/library/inspect.html

放弃这种倾向,停止走这条道路。

问题回答

如果没有上级机构,你就提到这一职能,而你是援引这一职能的结果。 这是沙尔辛奈的一个基本组成部分(这种一致性是一件好事!)

您可以永远使用<<<条码>property功能(或相关的代号),在世系下做一种方法,作为实现你所希望之财产的子。 然而,这表明,你撰写了非常副作用和重税的法典,这是“巴德理想”。

象这种链条一样,在“Qu”中很受欢迎,因为“ j”的唯一目的是在OM中产生副作用。 通常,这在其他地方是不明智的,因为它使你的申请很难被 de弄和测试。





相关问题
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 ]="...

热门标签