鱼C论坛

 找回密码
 立即注册
查看: 1580|回复: 1

[已解决]44题简单定制里面的问题,Python内部方法什么意思

[复制链接]
发表于 2017-7-29 12:15:08 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 likuo 于 2017-7-29 12:19 编辑

1、在44 简单定制里面,写了一个内部方法什么意思?不太懂
def _cal(self):
        self.lasted=[]
        self.prompt='总共运行了'
        for index in range(6):
        self.lasted.append(self.stop[index]-self.start[index])



2、for index in range(6):
        self.lasted.append(self.stop[index]-self.start[index])

为什么不可以直接写成self.lasted.append(self.stop[0:6]-self.start[0:6])  
最佳答案
2017-7-29 12:35:37
内部方法就是只能从类内部调用的方法,无法从外部调用
  1. >>> test = Test()
  2. >>> test.__clac()
  3. Traceback (most recent call last):
  4.   File "<pyshell#29>", line 1, in <module>
  5.     test.__clac()
  6. AttributeError: 'Test' object has no attribute '__clac'
  7. >>> test.call()
  8. 成功调用内部方法
复制代码


self.stop和self.start都是本质上元组,那么self.stop[0:6]和self.start[0:6]也是元组
元组与元组相减会报错
  1. >>> import time
  2. >>> last = time.localtime()
  3. >>> now = time.localtime()
  4. >>> now[0:6] - last[0:6]
  5. Traceback (most recent call last):
  6.   File "<pyshell#34>", line 1, in <module>
  7.     now[0:6] - last[0:6]
  8. TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
  9. >>>
复制代码

所以采用for循环将元素一个个提取出来计算~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-29 12:35:37 | 显示全部楼层    本楼为最佳答案   
内部方法就是只能从类内部调用的方法,无法从外部调用
  1. >>> test = Test()
  2. >>> test.__clac()
  3. Traceback (most recent call last):
  4.   File "<pyshell#29>", line 1, in <module>
  5.     test.__clac()
  6. AttributeError: 'Test' object has no attribute '__clac'
  7. >>> test.call()
  8. 成功调用内部方法
复制代码


self.stop和self.start都是本质上元组,那么self.stop[0:6]和self.start[0:6]也是元组
元组与元组相减会报错
  1. >>> import time
  2. >>> last = time.localtime()
  3. >>> now = time.localtime()
  4. >>> now[0:6] - last[0:6]
  5. Traceback (most recent call last):
  6.   File "<pyshell#34>", line 1, in <module>
  7.     now[0:6] - last[0:6]
  8. TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
  9. >>>
复制代码

所以采用for循环将元素一个个提取出来计算~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 05:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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