访问子类中的父级属性作为classMethod

发布于 2025-02-13 01:06:53 字数 1646 浏览 0 评论 0 原文

我有以下效果很好的代码:

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()

但是,我希望将最后一个方法定义为classMethod。因此,如果我进行以下更改,则代码会失败。

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()

我会出现一个错误,如下所示:

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

我意识到Python正在看儿童课 - 属性的学生。但是,通过继承,它也应查看父母应该找到属性的父。

我在这里想念什么?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2025-02-20 01:06:53

如果方法被标记为 @ClassMethod ,则仅具有对类的访问,而不能访问该类的实例。这意味着您无法访问实例变量。它与继承无关,您甚至无法在该方法中访问 gradyear 属性。表示 __ Init __()中设置的任何变量无法通过 @ClassMethod 访问。您可以阅读有关 @ClassMethod

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文