Python OOP from a Java programmer's perspective -


i have oop programming experience in java , have started working on project in python , i'm starting realize python makes me skeptical few things intuitive me. have few questions oop in python.

scenario: i'm writing program send emails. email, to, from, text , subject fields required , other fields cc , bcc optional. also, there bunch of classes implement core mail functionality, derive base class (mailer).

following 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):         = mailreq.from         = mailreq.to         subject= mailreq.subject         text = mailreq.text         options = getattr(mailreq,'options',none)         if(options != none):             if mailrequestoptions.bcc in options:                 #use property                 pass             if mailrequestoptions.cc in options:                 #use property                 pass    class mailrequest():     def __init__(self,from,to,subject,text):         self.from =         self.to =         self.subject = subject         self.text = text      def set_options(self,options):         self.options = options  class mailrequestoptions():     bcc = "bcc"     cc = "cc" 

questions:

  1. the send_mail method can take multiple parameters (from, to, subject, text, cc, bcc, etc.) , 4 of them required in app. since number of parameters method high, decided create wrapper object called mailrequest, have 4 necessary parameters properties, , other parameters may defined in options dictionary. problem is, here, looking @ code there's no way options is. dict or list? also, looking @ send_email method, there's no way tell mailreq is. is bad programming practice? should doing else? coming java world, makes me uncomfortable write code can't tell parameters looking @ code. got know annotations in python, don't want use them since they're supported in later versions.

  2. since options dict should used specify many other properties (cc , bcc 2 of them), i've created new class called mailrequestoptions of options may specified inside options dict mailrequestoptionss static strings. is bad practice well, or there better way this? not python specific, know.

  1. in python there no need create object. can use dictionary if want wrap mail request: mailreq = {'from': 'johnsmith@british.com', 'to': '....', ...}

  2. you should try use *args , **kwargs method. can make options simpler: def send_mail(from, to, subject, text, **kwargs), other options can retrieved using e.g. kwargs['bcc']. believe more pythonic.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -