Python如何在子类里扩展父类的property?,阿里巴巴Python面试题答案

本文探讨了在Python中子类如何正确扩展父类的property特性。通过实例展示了只覆盖getter或setter方法会导致的问题,并提供了解决方案,强调了必须显式使用父类的property装饰器以确保完整继承。同时,文章提到了Python在开发中的广泛应用和学习Python的重要性,分享了Python学习资源和大厂面试真题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sp = SubPerson(‘shy’)

I am in the SubPerson’s name setter

Traceback (most recent call last):

File “<pyshell#25>”, line 1, in

sp = SubPerson(‘shy’)

File “<pyshell#11>”, line 3, in init

self.name = name

File “<pyshell#24>”, line 9, in name

super().name = value

AttributeError: ‘super’ object has no attribute ‘name’

果然报错,提示super对象没有name属性,WTF!为什么可以get但是不能set?一直没有查到答案,最后help(super),才发现蛛丝马迹:

help(super)

Help on class super in module builtins:

class super(object)

| super() -> same as super(class, )

| super(type) -> unbound super object

| super(type, obj) -> bound super object; requires isinstance(obj, type)

| super(type, type2) -> bound super object; requires issubclass(type2, type)

| Typical use to call a cooperative superclass method:

| class C(B):

| def meth(self, arg):

| super().meth(arg)

| This works for class methods too:

| class C(B):

| @classmethod

| def cmeth(cls, arg):

| super().cmeth(arg)

|

| Methods defined here:

|

| get(self, instance, owner, /)

| Return an attribute of instance, which is of type owner.

|

| getattribute(self, name, /)

| Return getattr(self, name).

|

| init(self, /, *args, **kwargs)

| Initialize self. See help(type(self)) for accurate signature.

|

| new(*args, **kwargs) from builtins.type

| Create and return a new object. See help(type) for accurate signature.

|

| repr(self, /)

| Return repr(self).

|

| ----------------------------------------------------------------------

| Data descriptors defined here:

|

| self

| the instance invoking super(); may be None

|

| self_class

| the type of the instance invoking super(); may be None

|

| thisclass

| the class invoking super()

super本身只有__getattribute__,没有`setattr

,只对获取属性做了代理。因此设置的时候,会直接设置super()对象本身的属性,所以出现如上的错误提示,因此只能够显式调用name的set`方法。。。。

另外一个坑就是如果子类全面扩展父类的property,可以用上面的方法,但是如果只是扩展get或者set方法,就不行了,如下:

‘’’

遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939

寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!

‘’’

class SubPerson(Person):

@property

def name(self):

print(“I am in SubPerson’s getter”)

super().name

sp = SubPerson(‘shy’)

Traceback (most recent call last):

File “<pyshell#48>”, line 1, in

sp = SubPerson(‘shy’)

File “<pyshell#11>”, line 3, in init

self.name = name

AttributeError: can’t set attribute

父类的setter方法消失了,这里比较好理解,property是描述符,是get,set,delete的集合,子类仅仅只设置了get,set和delete相当于根本没有设置。如果想要继承父类的property,只能显式的用父类的property来装饰,如下:

最后

Python崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS等更加高级的领域。Python可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

👉Python所有方向的学习路线👈

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

👉Python必备开发工具👈

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

👉Python全套学习视频👈

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

👉实战案例👈

学python就与学数学一样,是不能只看书不做题的,直接看步骤和答案会让人误以为自己全都掌握了,但是碰到生题的时候还是会一筹莫展。

因此在学习python的过程中一定要记得多动手写代码,教程只需要看一两遍即可。

👉大厂面试真题👈

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


31c50236a205928a1d8ae8a0b883.png)

👉大厂面试真题👈

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值