|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- >>> class C:
- ... def __init__(self, name, age):
- ... self.name = name
- ... self.__age = age
- ... def __getattribute__(self, attrname):
- ... print("拿来吧你~")
- ... return super().__getattribute__(attrname)
- ...
- >>> c = C("小甲鱼", 18)
- >>> getattr(c, "name")
- 拿来吧你~
- >>> c._C__age
- 拿来吧你~
- 18
复制代码
您好,请问上面这些代码中。
getattr(c, "name") 这条语句运行结果有问题,我觉得应该是
拿来吧你~
小甲鱼
但是实际运行结果是:
拿来吧你~
请问是为什么呢?
本帖最后由 三体人的智子 于 2024-7-31 07:00 编辑
你的代码没有问题,运行后和你想的一样,没有问题
- class C:
- def __init__(self,name,age): #注意下划线有两个,"__" 不是"_"
- self.name=name
- self.__age=age
- def __getattribute__(self,attrname):
- print("拿来把你")
- return super().__getattribute__(attrname) #注意下划线两个!不要写成"_"
- c=C("小甲鱼",18)
- getattr(c,"name")
- 拿来把你
- '小甲鱼'
复制代码
请检查你的代码,或更改编译器
|
|