与Python的流畅界面

编程入门 行业动态 更新时间:2024-10-11 17:28:14
本文介绍了与Python的流畅界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Python函数 send_message,该函数带有三个参数:

I have a Python function "send_message" which takes three arguments:

send_message("i like windmills", to="INBOX", from="OUTBOX")

我正在考虑将流利的界面放在它。理想情况下,我想编写以下代码:

I am thinking about putting a fluent interface on top of it. Ideally I'd like to write the following:

send_message("i like windmills").to("INBOX").from("OUTBOX")

send_message("i like windmills").from("OUTBOX").to("INBOX")

更新: to()信息是强制性的,但 from()不是(与真实字母一样)。因此,这也将是一个有效的调用:

Update: The to() information is mandatory but the from() is not (as with real letters). So this one would also be a valid call:

send_message("i like windmills").to("INBOX")

任何想法如何实现这一目标或类似目标?

Any ideas how to accomplish this or something similar?

让我理解具有使对象的方法返回自身的方法的一般方法,但根据我的理解,这将导致以下情况:

The general approach of having methods of an object returning "self" is understood by me but in my understanding this would lead to something like this:

message = Message("i like windmills") message.to("INBOX").from("OUTBOX").send()

但是这个不如前面的示例好,所以我实际上更喜欢原始版本

But this one is not as nice as the previous example and I then would actually prefer the original version with the named arguments.

不胜感激。

推荐答案

可以通过这种方式完成,我不确定是否有更好的方法,因为这是我的第一次尝试。

It can be accomplished this way, I am unsure if there is a better way because this is my first attempt. Good luck!

DEFAULT_SENDER = 'my_address' #Because the sender object is optional I assume you have a default sender class Send_message(object): def __init__(self, message): self.message = message self.sender = None self.receiver = None self.method = None def to(self, receiver): self.receiver = receiver self.method = self.send() return self def _from(self, sender): self.sender = sender self.method = self.send() return self def __call__(self): if self.method: return self.method() return None def send(self): if self.receiver: if not self.sender: self.sender = DEFAULT_SENDER return lambda:actual_message_code(self.message, self.sender, self.receiver) def actual_message_code(message, sender, receiver): print "Sent '{}' from: {} to {}.".format(message, sender, receiver) Send_message("Hello")._from('TheLazyScripter').to('samba2')() Send_message("Hello").to('samba2')._from('TheLazyScripter')() Send_message("Hello").to('samba2')() #Only change in actual calling is the trailing ()

通过实现 __ call __ 方法,我们可以知道何时到达调用链的末尾。当然,这会添加尾随()调用。并要求您更改指向实际消息传递方法的指针和默认的sender变量,但是我认为这是实现目标的最简单方法,而无需实际知道链何时结束。

By implementing the __call__ method we can tell the when we are at the end of the call chain. This of course adds the trailing () call. and requires you to change the pointer to your actual messaging method and default sender variable but I feel that this would be the simplest way to accomplish your goals without actually knowing when the chain ends.

更多推荐

与Python的流畅界面

本文发布于:2023-11-17 09:10:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流畅   界面   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!