看流畅的python感觉有难度,与Python的流畅界面

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

or

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

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.

Any help is appreciated.

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值