[Fixed]-New object instance every call

1👍

This would be the recommended structure:

from my_app.utils import com
com.Mail().email_category1(template, ...)

where my_app.utils.com is:

Mail = CommunicationMail

If you really wanted to keep the com.mail.email_category1 notation, Python would let you, of course, being the dynamic language that it is
(__getattr__ documentation):

# my_app.utils.com

class CommunicationMailFactory:
    def __getattr__(self, name):
        instance = CommunicationMail()
        return getattr(instance, name)

mail = CommunicationMailFactory()

But use the first method! “Why,” you ask.

For one, it is makes it clear what you are doing: You are instantiating a new instance and calling a method. This is not clear with the __getattr__ hackery.

Second, you can assign the freshly instantiated instance to a variable mail1 and then call mail1.email_category1(subject, template, ...) or whatever. You have no such normal, expected flexibility with the __getattr__ hackery.

0👍

Python modules are singleton, so it will only import it once, so mail = CommunicationMail() is executed once.

you can:

from my_app.utils import com

com.CommunicationSms().sms_category1(template, ...)

Leave a comment