0

I am writing a custom email helper for personal convenience in my django app. But I realize there are 2 ways of doing the same thing.

By importing a class and calling its @staticmethod

# email_helper.py
from django.core.mail import send_mail
class EmailHelper(object):
    @staticmethod
    def send(subject, recipients, static_message, html_message):
        send_mail(
            subject = subject,
            from_email = "Bob <bob@example.com>",
            recipient_list = recipients,
            message = static_message,
            html_message = html_message,
            fail_silently = False,
        )

# usage:
from email_helper import EmailHelper
EmailHelper.send(subject="", recipients=[], static_message="", html_message="")
# end_of_usage

By importing a module and calling a function in it

# email_helper.py
from django.core.mail import send_mail
def send(subject, recipients, static_message, html_message):
    send_mail(
        subject = subject,
        from_email = "Bob <bob@example.com>",
        recipient_list = recipients,
        message = static_message,
        html_message = html_message,
        fail_silently = False,
    )

# usage:
import email_helper
email_helper.send(subject="", recipients=[], static_message="", html_message="")
# end_of_usage

What would be the runtime / compile-time / operational / any difference between these two approaches?

P.S. I have actually tried both the approach and they both work fine for my use case. However, I don't have much production-level workload at the moment to objectively benchmark real performance benefits right now. My curious mind is trying to understand the anatomy and the science behind these 2 approaches on python ;-)

P.S. There is another StackOverflow question which is somewhat close to the concern that this question is trying to ask. Module function vs staticmethod vs no decorator . However, i found that other question to be a bit too broad while this question is relatively more specific.

Community
  • 1
  • 1
Rakib
  • 12,376
  • 16
  • 77
  • 113
  • Probably not much, why don't you just pick one and see how it goes? – jonrsharpe Mar 05 '16 at 10:38
  • i have actually tried both and they both work fine to my usecase. I don't have much production-level workload though to benchmark real performance differences. – Rakib Mar 05 '16 at 10:39
  • In that case, why worry about it until it actually becomes a problem (which it may never do)? – jonrsharpe Mar 05 '16 at 10:39
  • lol... right.... not worrying about it..... curious mind is trying to understand the anatomy and the science behind these 2 approaches on python ;-) – Rakib Mar 05 '16 at 10:42
  • 1
    Classes in Python are runtime objects, too. So invoking your example's module function requires one less object than invoking the `@staticmethod`: The `EmailHelper` class. Then there's probably some objects (at least an additional function object that wraps the original function) involved in the processing of the decorator (`@...`). Finally, as the article cited in http://stackoverflow.com/questions/11788195/module-function-vs-staticmethod-vs-classmethod-vs-no-decorators-which-idiom-is mentions, there's one namespace lookup more to get to the function to invoke for the staticmethod case. – das-g Mar 05 '16 at 11:05
  • 1
    Despite all that, the performance difference should be minimal enough to be insignificant. More so, compared to the load caused by sending an email. Thus prefer what is more readable and comprehensible. For textual readability, there's not a big difference, IMHO. As the `EmailHelper` class doesn't represent an important concept that its instances implement, and to which the function or method is strongly related, nesting the function into the class doesn't help to comprehend what it does. – das-g Mar 05 '16 at 11:11
  • 1
    The module is already helpfully called `email_helper`, so `email_helper.send(...)` is really all a reader needs to see to figure out what is going on. A class name won't improve it (might even be confusing) so I'd prefer the plain module function. – das-g Mar 05 '16 at 11:13
  • @das-g Really nicely illustrated. All 3 of your comments together are very much worthy of being a SO answer for this question. In fact, as an answer, it would help other curious minds too. :) – Rakib Mar 05 '16 at 11:19
  • attention @jonrsharpe... we are having some credible discussion here. So instead of closing this question by referencing a more sophisticated answer containing 4 different things, can we allow this question to be open as it has discussion around only 2 different things? I can of course edit this questoin to include the link to the other question you have referred here. – Rakib Mar 05 '16 at 11:28
  • I would have posted my comments as an answer if the question wasn't closed as a duplicate. As they are partially specific to your case, they wouldn't be a valid answer to the allegedly [duplicated question](http://stackoverflow.com/questions/11788195/module-function-vs-staticmethod-vs-classmethod-vs-no-decorators-which-idiom-is), thus I didn't post them there. – das-g Mar 05 '16 at 11:33

0 Answers0