0

I have a question. I am watching class tutorials and cam across class methods. My question can be better understood after seeing the code. I will show the code first.

The Code:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        self.from_lastname()


    def from_lastname(self):
        Person.mostpreviouslastname = self.lastname

p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)

p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)

That was the part without class methods. This is with:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        Person.from_lastname(self.lastname)


    @classmethod
    def from_lastname(cls, last_name1):
        cls.mostpreviouslastname = last_name1

p1 = Person('Mike', 'Wizoski')
print(Person.mostpreviouslastname)
print(p1.lastname)

p2 = Person ('Indiana', 'Jones')
print(p1.lastname)
print(p2.lastname)
print(Person.mostpreviouslastname)

They both work the same. So my question is why even use class methods at all?

BeastCoder
  • 2,391
  • 3
  • 15
  • 26
  • Methods want to do something with an instance of a class, a classmethod wants to do something with a class itself (like use it to make an instance for example) – donkopotamus Aug 10 '18 at 01:30

2 Answers2

2

Classmethods in python make more sense when you consider inheritance. Let's use a slightly more evocative example:

Say you have a class Item that represents an item in your grocery store. Let's also say that all Items can come in a two-pack. We'll represent that with a classmethod that returns a tuple.

class Item:
    @classmethod
    def two_pack(cls):
        return cls(), cls()

Now let's say you want to subclass Item to really represent specific items, like Soda

class Soda(Item):
    pass

Now we can call Soda.two_pack() and get a tuple of Soda objects, even though the two_pack method doesn't know about Soda objects at all.

This also touches on the main use of class methods, which is providing alternate means of building an object, so you're not limited to what firs in __init__

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

In your example you likely wouldn't want to use class methods. That's because there can only be one value of fullname and it's attached to the Person class object. So if you ever had more than one instance of Person, then they couldn't have separate fullname values.

In your case you'd want to remove the class method and have from_fullname reference self, so that the names are different for each Person instance:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname
        self.from_fullname()

    def from_fullname(self):
        self.fullname = self.lastname


p1 = Person('Mike', 'Wizoski')
p2 = Person('Bob', 'Jones')
print(p1.fullname)
print(p2.fullname)
# Wizoski
# Jones

If you stuck with either of your examples, you'd get the wrong result when instantiating more than one person:

# Jones
# Jones

So when would you use class methods? When you have some function that doesn't use any of the instance's data (i.e. doesn't need a self reference). More detailed explanations can be found here:

101
  • 8,514
  • 6
  • 43
  • 69
  • This is not exactly what I’m looking for. I changed the question a bit to make it easier to understand. Thx, though. – BeastCoder Aug 10 '18 at 10:35