DEV Community

Discussion on: 30 Days of Python 👨‍💻 - Day 8 - OOP Basics

Collapse
 
petrtcoi profile image
Petr Tcoi

Thank you

But I mean the difference between class method and class method with @classmethod

Thread Thread
 
arindamdawn profile image
Arindam Dawn

Oops, I probably misunderstood your question then!

Ok let me try to explain the difference:

Here is a class with a simple method

class Avenger:
  def __init__(self, name, weapon):
    self.name = name
    self.weapon = weapon

  def fight(self):
    print(self.weapon)

  @classmethod
  def showOrganization(cls):
    print('MARVEL INC')

spiderman = Avenger('Spiderman', 'dispatch a web')

spiderman.fight() # dispatch a web

Avenger.showOrganization() # this works
spiderman.showOrganization() # this also works

Here what I am trying to show is you can know the Avenger's organization name by simply calling the class method. You can also create an avenger and then get the name of the organization from that avenger but hope you understand the difference!

Thread Thread
 
petrtcoi profile image
Petr Tcoi

Great thank you for the example!