鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 2

[已解决]pycharm中__getattr__与__getattribute__的问题

[复制链接]
发表于 2021-6-16 18:51:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
与视频代码一样,但是在pycharm中运行好像不正确。
代码如下:
class C(object):

    def __getattribute__(self, name):
        print('getattribute')
        return super(object).__getattribute__(name)

    def __getattr__(self, name):
        print('getattr')

    def __setattr__(self, name, value):
        print('setattr')
        super(object).__setattr__(name, value)

    def __delattr__(self, name):
        print('delattr')
        super(object).__delattr__(name)

在实例化之后就成了这样:
c1 = C()
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattr
getattribute
getattr
getattribute
getattr
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattr
getattribute
getattr
getattribute
getattr
getattribute
getattribute
getattribute
getattr
getattribute
getattr
getattribute
getattr
getattribute
getattribute
getattribute
getattribute
getattribute
getattribute
getattr
getattribute
getattr
getattribute
getattr
-----------------------------------------------------------------------------------------------------------------------
但是在python IDLE中就运行正确。
pycharm用的python解释器为python3.7。
请问有没有人知道是为什么
最佳答案
2021-6-16 19:00:22


PyCharm 或者其他编译器的控制台,打印多次是正常现象

可能是因为 PyCharm 之类的编译器有着完整的内置环境

所以不必在意,这个不影响正常运行代码

而且不止 __getattr__ ,连 __setattr__ 之类的也会这样

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-16 19:00:22 | 显示全部楼层    本楼为最佳答案   


PyCharm 或者其他编译器的控制台,打印多次是正常现象

可能是因为 PyCharm 之类的编译器有着完整的内置环境

所以不必在意,这个不影响正常运行代码

而且不止 __getattr__ ,连 __setattr__ 之类的也会这样

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-16 19:12:16 | 显示全部楼层
Twilight6 发表于 2021-6-16 19:00
PyCharm 或者其他编译器的控制台,打印多次是正常现象

可能是因为 PyCharm 之类的编译器有着完整的 ...

谢谢!我试着不用console直接输出就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 04:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表