Disclaimer: This question is very much like the one posted here. I gathered some opinions about my options from the answer there. Here, I just want validation about the choices I'm deciding to stick to and see what people think about the decisions specifically. Also, gather suggestions about other parts of the code I'm not specifically asking about, since I'm new to Python OOP.
Scenario: I'm writing a program that will send emails. For an email, the to
, from
, text
and subject
fields will be required and other fields like cc
and bcc
will be optional. Also, there will be a bunch of classes that will implement the core mail functionality, so they will derive from a base class (Mailer
).
Following is my incomplete code snippet:
class Mailer(object):
__metaclass__ == abc.ABCMeta
def __init__(self,key):
self.key = key
@abc.abstractmethod
def send_email(self, mailReq):
pass
class MailGunMailer(Mailer):
def __init__(self,key):
super(MailGunMailer, self).__init__(key)
def send_email(self, mailReq):
from = mailReq.from
to = mailReq.to
subject= mailReq.subject
text = mailReq.text
options = getattr(mailReq,'options',None)
if(options != None):
if MailRequestOptions.BCC in options:
#use this property
pass
if MailRequestOptions.CC in options:
#use this property
pass
class MailRequest():
def __init__(self,from,to,subject,text):
self.from = from
self.to = to
self.subject = subject
self.text = text
def set_options(self,options):
self.options = options
class MailRequestOptions():
BCC = "bcc"
CC = "cc"
I've made the following decisions about code designs. What do you think about them?
The
send_email()
method will take four required parameters - to, from, subject and text, and bunch of other optional parameters like cc, bcc etc. So I decided to create theMailRequest
wrapper, which will take the 4 required fields in the constructor, and the other optional parameters will go in the options dict.Is this an acceptable way of doing this? Why not use
**kwargs
?Because if you define something like:
def foo(**kwargs): pass
Then to call it, you can't do something like:
options = [] options["one"] = 1 options["two"] = 2 foo(options)
Right?
You'd have to do something like:
foo(one=1,two=2)
Now if I have 15 parameters that foo accepts, then
**kwargs
isn't a good way to do this right?I've created the
MailRequestOptions
class to contain static strings. The reason behind the existence of this class is, even if the user knows he has to pass some options in the options dict of theMailRequest
object, how would he know which options can he set. This class could probably help the user know about what options can be set. This will also be helpful if the user has auto complete in an IDE or something. Do you think I'm thinking right? Or is this a somewhat unusual way of doing things?