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 any of the following:
send_message("i like windmills").to("INBOX").from("OUTBOX")
send_message("i like windmills").from("OUTBOX").to("INBOX")
# 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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…