鱼C论坛

 找回密码
 立即注册
查看: 1244|回复: 13

想要输出I love FishC,..from class{cls.__name__}确输出了<bound method ? of

[复制链接]
发表于 2023-2-5 22:03:10 | 显示全部楼层 |阅读模式
5鱼币
class D:
        @classmethod
        @property
        def __doc__(cls):
                return f"I love FishC,..from class{cls.__name__}"

       
d=D()
d.__doc__

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

使用道具 举报

 楼主| 发表于 2023-2-5 22:03:28 | 显示全部楼层
请求大佬帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-5 22:06:22 | 显示全部楼层
本帖最后由 isdkz 于 2023-2-5 22:32 编辑

这个输出有问题吗?

>>> class D:
...         @classmethod
...         @property
...         def __doc__(cls):
...                 return f"I love FishC,..from class{cls.__name__}"
...
>>>
>>> d=D()
>>> d.__doc__
'I love FishC,..from classD'
>>>




输出了 <bound method  应该是你漏掉了 @property

>>> class D:
...         @classmethod
...         def __doc__(cls):
...                 return f"I love FishC,..from class{cls.__name__}"
...
>>>
>>> d=D()
>>> d.__doc__
<bound method D.__doc__ of <class '__main__.D'>>
>>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-5 22:12:17 | 显示全部楼层
  1. class D:
  2.     'I love FishC,..from class{cls.__name__}'
  3.     @classmethod
  4.     @property
  5.     def test(self):
  6.         pass


  7.       
  8. d=D()
  9. print(d.__doc__)
复制代码



每个对象都会有一个__doc__属性,用于描述该对象的作用。在一个模块被import时,其文件中的某些特殊的字符串会被python解释器保存在相应对象的__doc__属性中。比如,一个模块有模块的__doc__,一个class或function也有其对应的__doc__属性。在python中,一个模块其实就是一个.py文件。在文件中特殊的地方书写的字符串就是所谓的docstrings,就是将被放到__doc__的内容。这个“特殊的地方”包括:

1. 一个文件任何一条可执行的代码之前  #模块的__doc__

2. 一个类,在类定义语句后,任何可执行代码前#类的__doc__

3. 一个函数,在函数定义语句后,任何可执行代码前#函数的__doc__

举个例子:

  1. #use  __doc__ 属性
  2. class MyClass:
  3.     'string.'
  4.     def printSay():
  5.         'print say welcome to you.'
  6.         print 'say welcome to you.'
  7. print MyClass.__doc__
  8. print MyClass.printSay.__doc__
  9.   
复制代码

#输出结果
string.
print say welcome to you.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-5 22:15:31 | 显示全部楼层
  1. class D:
  2.         @classmethod
  3.         @property
  4.         def __doc__(cls):
  5.                 return f"I love FishC,..from class {cls.__name__}"
  6.       
  7. d=D()
  8. print(d.__doc__)
复制代码

运行:
  1. I love FishC,..from class D
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-6 11:50:46 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-2-6 11:53 编辑

Def doc代表doc是一个类的方法(method)
D.__doc__是实力d的doc属性/方法
所以他会提示doc是方法(函数)
(打字问题请见谅)
  1. #改成
  2. class D:
  3.     __doc__ = ' I love fishc, from class ' + __name__
  4. print(D().__doc__)
复制代码

或者类部分不动,其他改成
print(D().__doc__())
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-6 14:36:17 | 显示全部楼层
isdkz 发表于 2023-2-5 22:06
这个输出有问题吗?

第3行@property,但是结果还是显示bound method
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-6 14:42:29 | 显示全部楼层
18305177067 发表于 2023-2-6 14:36
第3行@property,但是结果还是显示bound method

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

使用道具 举报

 楼主| 发表于 2023-2-6 15:21:31 | 显示全部楼层
歌者文明清理员 发表于 2023-2-6 11:50
Def doc代表doc是一个类的方法(method)
D.__doc__是实力d的doc属性/方法
所以他会提示doc是方法(函数 ...

代码没问题。输出结果为I love FishC,..from class{cls.__name__},其中{cls.__name__}无法改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-6 15:22:59 | 显示全部楼层
18305177067 发表于 2023-2-6 15:21
代码没问题。输出结果为I love FishC,..from class{cls.__name__},其中{cls.__name__}无法改变

很奇怪的问题。在字符串前面加f了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-6 15:24:01 | 显示全部楼层
歌者文明清理员 发表于 2023-2-6 15:22
很奇怪的问题。在字符串前面加f了吗


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

使用道具 举报

发表于 2023-2-6 16:54:10 | 显示全部楼层
The code you posted will result in an AttributeError, because the __doc__ property of the D class is being redefined as a class method decorated with the property decorator.

By using the property decorator, you are indicating that the __doc__ method should behave as a read-only property. However, the __doc__ attribute is already a built-in property of Python classes that provides the docstring for the class.

If you want to define a custom property for the D class, you should give it a different name, such as doc, and use the property decorator to define it:
  1. class D:
  2.     @property
  3.     def doc(self):
  4.         return f"I love FishC,..from class{self.__class__.__name__}"

  5. d = D()
  6. print(d.doc)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-6 17:12:44 | 显示全部楼层
ouyunfu 发表于 2023-2-6 16:54
The code you posted will result in an AttributeError, because the __doc__ property of the D class is ...

我去,6

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
歌者文明清理员 + 1 + 1 评分区间是-1~1,就算还你之间给我的积分.

查看全部评分

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

使用道具 举报

发表于 2023-2-6 18:10:08 | 显示全部楼层
ouyunfu 发表于 2023-2-6 16:54
The code you posted will result in an AttributeError, because the __doc__ property of the D class is ...

这是故意说英文?
百度百度开始翻译

评分

参与人数 1荣誉 +1 贡献 +1 收起 理由
sfqxx + 1 + 1 没事

查看全部评分

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 00:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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