0

Example 1:

class names:   
    def myname(self):       
        return 'my name is python'  
word = names().myname()
print(word)

Result:

my name is python

Example 2:

class names:   
    @classmethod
    def myname(cls):
        cls.justaname = 'my name is python'
        return cls.justaname
word = names().myname()
print(word)

Result:

my name is python

1 - What is the difference between the two methods ?

2 - What changes did '@classmethod' make to the second example ?

3 - I can make the second example work without '@classmethod' decorator so what is the point of adding it is it useless ?

  • Try `names.myname()` on both examples. – Gino Mempin Sep 04 '22 at 21:24
  • [What is the purpose of @classmethod in Python?](https://stackoverflow.com/q/64496373/2745495) – Gino Mempin Sep 04 '22 at 21:28
  • everything works perfectly but what is the difference ? both programs give the same result so can you tell me what is the difference between using '@classmethod' and without using '@classmethod' ? –  Sep 04 '22 at 21:29
  • even if i don't use '@classmethod' python knows that this function doesn't belong to any instance i don't see any difference do you see any difference ? –  Sep 04 '22 at 21:32
  • Like I said (and what's explained in the links to the other questions), `names.myname()` (notice no `()` on `names`, not creating an instance of `names`) will _only_ work on Example 2. That's the difference. – Gino Mempin Sep 04 '22 at 21:35

0 Answers0