1

I tried to find out the difference between 1) @classmethod and 2) invoking the class inside a method. But, they output the same result and their execution time's pretty much identical. Here is the code:

import time

class Student(object):

    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @classmethod
    def from_string(cls, name_str):
        first_name, last_name = map(str, name_str.split(' '))
        student = cls(first_name, last_name)
        return student
    
    def from_string2(name_str):
        first_name, last_name = map(str, name_str.split(' '))
        student = Student(first_name, last_name)
        return student

st = time.time()
scott = Student.from_string('Scott Robinson')
et = time.time()
print(f'{et-st:.8f}')

st = time.time()
scott2 = Student.from_string2('Scott Robinson')
et = time.time()
print(f'{et-st:.8f}')

Output>>> 0.00004053
Output>>> 0.00003552

I'm curious why @classmethod is preferred to invoking the class inside a method? Please enlighten me.

Kay
  • 43
  • 1
  • 7
  • Does this answer your question? [What is the purpose of class methods?](https://stackoverflow.com/questions/38238/what-is-the-purpose-of-class-methods) or [why should I use @classmethod when I can call the constructor in the without the annotation?](https://stackoverflow.com/questions/62072947/why-should-i-use-classmethod-when-i-can-call-the-constructor-in-the-without-the) – TheFungusAmongUs Jul 29 '22 at 01:59
  • @TheFungusAmongUs. Yes certainly it did. Thank you very much. For anyone who's interested in it, check this out: [link](https://stackoverflow.com/a/62073365/10798490) – Kay Jul 29 '22 at 02:43
  • @TheFungusAmongUs I tried looking through the existing questions and I'm not sold that anything I could find was a good duplicate, although I strongly suspect there's one out there. I'm going to go ahead and write an answer provisionally. – Karl Knechtel Jul 29 '22 at 02:48
  • @KarlKnechtel Personally, and as Kay mentioned, I think the second one would have been the right dupe target. However, your answer improves over the answer in the other post and so I'm tempted to flag that question as a dupe of this one. – TheFungusAmongUs Jul 29 '22 at 02:58
  • Hmm, I'm not so sure. It might be better for me to migrate my answer there (adopting it to the new context). I didn't want to use something so obscure as a duplicate target, but it might be the closest match available. The answers should be all in the same place, and I think that question is better asked than this one. We can look for something better later. That said, anything that e.g. contrasts `@classmethod` with `@staticmethod`, and doesn't mention what happens with undecorated, `self`-less methods, is definitely not good enough here. – Karl Knechtel Jul 29 '22 at 03:06
  • I went ahead and moved my answer there, adapting it to the `Person` example, and made the duplicate link. – Karl Knechtel Jul 29 '22 at 03:16

0 Answers0