I only have OOP programming experience in Java and have just started working on a project in Python and I'm starting to realize Python makes me skeptical about a few things that are intuitive to me. So I have a few questions about OOP in Python.
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"
Questions:
The
send_mail
method can take multiple parameters (from, to, subject, text, cc, bcc
, etc.) and only four of them are really required in my app. Since the number of parameters for the method is too high, I decided to create a wrapper object calledMailRequest
, which will have the four necessary parameters as properties, and all other parameters may be defined in theoptions
dictionary. Problem is, here, just looking at the code there's no way to say thatoptions
is. Is it adict
or alist
? Also, looking at thesend_email
method, there's also no way to tell whatmailReq
is. Is this bad programming practice? Should I be doing something else? Coming from the Java world, it makes me very uncomfortable to write code where you can't tell what the parameters are by just looking at the code. I got to know about annotations in Python, but I don't want to use them since they're only supported in later versions.Since the
options
dict should be used to specify many other properties (cc
andbcc
are just two of them), I've created a new class calledMailRequestOptions
with all of the options that may be specified inside the options dict asMailRequestOptions
s static strings. Is this bad practice as well, or is there a better way to do this? This is not really Python specific, I know.