Home > Mobile >  Solved : Accessing Parent Class Attributes in Child Class as classmethod
Solved : Accessing Parent Class Attributes in Child Class as classmethod

Time:07-06

I have the following code which works fine :

class Person:
    def __init__(self, fname, lname) -> None:
        self.firstname = fname
        self.lastname = lname

    def __repr__(self) -> str:
        return f'{self.firstname} {self.lastname}'

class Student(Person):
    def __init__(self, fname, lname, year) -> None:
        super().__init__(fname, lname)
        self.gradyear = year

    def __repr__(self) -> str:
        return f'{self.firstname} {self.lastname} passed in {self.gradyear}'

    def welcome(self):
        print(f'Welcome, {self.firstname} {self.lastname} you passed out in {self.gradyear}')

x = Person("John", "Doe")
y = Student("John", "Doe", 1988)

y.welcome()

However, I wish to define the last method - welcome - as a classmethod. So if I do the following change, the code fails.

class Person:
    def __init__(self, fname, lname) -> None:
        self.firstname = fname
        self.lastname = lname

    def __repr__(self) -> str:
        return f'{self.firstname} {self.lastname}'

class Student(Person):
    def __init__(self, fname, lname, year) -> None:
        super().__init__(fname, lname)
        self.gradyear = year

    def __repr__(self) -> str:
        return f'{self.firstname} {self.lastname} passed in {self.gradyear}'

    @classmethod
    def welcome(cls):
        print(f'Welcome, {cls.firstname} {cls.lastname} you passed out in {cls.gradyear}')

x = Person("John", "Doe")
y = Student("John", "Doe", 1988)

y.welcome()

I get an error as follows :

AttributeError: type object 'Student' has no attribute 'firstname'

I realize that Python is looking at the child class - Student for the attributes. However, by inheritance, it should also look at the Parent, where it should find the attributes.

What am I missing here ?

CodePudding user response:

If method is marked as @classmethod, it has only access to the class and not the instance to the class. Meaning you don't have access to instance variables. It has nothing to do with inheritance, you cannot even access gradyear attribute in that method. Meaning any variables set in __init__() cannot be access by @classmethod. You can read more about @classmethod here.

  • Related