鱼C论坛

 找回密码
 立即注册
查看: 1531|回复: 3

[已解决]请问这里最后一行代码为啥没有调动property里面的doc?

[复制链接]
发表于 2021-9-14 15:55:23 | 显示全部楼层 |阅读模式
10鱼币
  1. class Foo:
  2.     def __init__(self,num):
  3.         self.age = num
  4.     @property
  5.     def get(self):
  6.         return self.age
  7.     @get.setter
  8.     def get(self,num1):
  9.         self.age = num1
  10. class temp:
  11.     def __init__(self,time):
  12.         self.count = time
  13.     def get(self):
  14.         return self.count
  15.     def set(self,time1):
  16.         self.count = time1
  17.     def delete(self):
  18.         del self.count
  19.         print ('已删除')
  20.     p = property(get,set,delete,'修饰符property的使用')

  21. #a = Foo(10)
  22. #print (a.get)
  23. #a.get = 20
  24. #print (a.get)
  25. b = temp(5)
  26. print (b.p)
  27. b.p = 10
  28. print (b.p)
  29. print (b.__dict__)
  30. del b.p
  31. print (b.__dict__)
  32. b.p.__doc__
复制代码
最佳答案
2021-9-14 15:55:24
你用 类名.p.__doc__  就行了。。
  1. In [1]: class temp:
  2.    ...:     def __init__(self,time):
  3.    ...:         self.count = time
  4.    ...:     def get(self):
  5.    ...:         return self.count
  6.    ...:     def set(self,time1):
  7.    ...:         self.count = time1
  8.    ...:     def delete(self):
  9.    ...:         del self.count
  10.    ...:         print ('已删除')
  11.    ...:
  12.    ...:     p = property(get,set,delete,'修饰符property的使用')
  13.    ...:

  14. In [2]: t = temp()
  15. In [3]: t = temp(60)

  16. In [4]: t.p
  17. Out[4]: 60

  18. In [5]: t.__doc__

  19. In [6]: temp.__doc__

  20. In [7]: temp.p.__doc__
  21. Out[7]: '修饰符property的使用'
复制代码



# 至于为什么是这样子? 我也不想去进一步了解,哈哈,反正这样的装饰器的__doc__特性,几乎用不上。更常用的是直接用@property



最佳答案

查看完整内容

你用 类名.p.__doc__ 就行了。。 # 至于为什么是这样子? 我也不想去进一步了解,哈哈,反正这样的装饰器的__doc__特性,几乎用不上。更常用的是直接用@property
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-14 15:55:24 | 显示全部楼层    本楼为最佳答案   
你用 类名.p.__doc__  就行了。。
  1. In [1]: class temp:
  2.    ...:     def __init__(self,time):
  3.    ...:         self.count = time
  4.    ...:     def get(self):
  5.    ...:         return self.count
  6.    ...:     def set(self,time1):
  7.    ...:         self.count = time1
  8.    ...:     def delete(self):
  9.    ...:         del self.count
  10.    ...:         print ('已删除')
  11.    ...:
  12.    ...:     p = property(get,set,delete,'修饰符property的使用')
  13.    ...:

  14. In [2]: t = temp()
  15. In [3]: t = temp(60)

  16. In [4]: t.p
  17. Out[4]: 60

  18. In [5]: t.__doc__

  19. In [6]: temp.__doc__

  20. In [7]: temp.p.__doc__
  21. Out[7]: '修饰符property的使用'
复制代码



# 至于为什么是这样子? 我也不想去进一步了解,哈哈,反正这样的装饰器的__doc__特性,几乎用不上。更常用的是直接用@property



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

使用道具 举报

发表于 2021-9-16 12:36:17 | 显示全部楼层

b.p 对象都被你 del 删除了,再对该对象 __doc__ 肯定会报错的

把 b.p.__doc__ 代码放再 del b.p 前一行即可

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

使用道具 举报

 楼主| 发表于 2021-9-16 20:17:57 | 显示全部楼层
Twilight6 发表于 2021-9-16 12:36
b.p 对象都被你 del 删除了,再对该对象 __doc__ 肯定会报错的

把 b.p.__doc__ 代码放再 del b.p 前一 ...
  1. class temp:
  2.     def __init__(self,time):
  3.         self.count = time
  4.     def get(self):
  5.         return self.count
  6.     def set(self,time1):
  7.         self.count = time1
  8.     def delete(self):
  9.         del self.count
  10.         print ('已删除')
  11.     p = property(get,set,delete,'修饰符property的使用')

  12. #a = Foo(10)
  13. #print (a.get)
  14. #a.get = 20
  15. #print (a.get)
  16. b = temp(5)
  17. #print (b.p)
  18. #b.p = 10
  19. #print (b.p)
  20. #print (b.__dict__)
  21. #del b.p
  22. #print (b.__dict__)
  23. print (b.p.__doc__)
复制代码


我改成运行后返回:
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is a number, return x.__int__().  For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base.  The literal can be preceded by '+' or '-' and be surrounded
by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4

没懂为啥会返回这个,我想让他返回property里面我输入的文本
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 12:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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