鱼C论坛

 找回密码
 立即注册
查看: 1969|回复: 8

请问这个属性名和方法名就不冲突吗?

[复制链接]
发表于 2019-9-1 21:27:31 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class MyTimer:
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始...")
    #停止计时
    def stop(self):
        self.stop = t.localtime()
        print("计时结束!")

    #停止计时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时结束!")

   
    # 内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt += str(self.lasted[index])

        print(self.prompt)

请问:
    def start(self):
        self.start = t.localtime()
这两句的start 属性名和方法名不冲突吗


假如在开始再增加
    def __init__(self):
        self.prompt = "未开始计时!"
        self.lasted = []
        self.start = 0
        self.stop = 0

运行时候就会提示
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    t1.start()
TypeError: 'int' object is not callable
此时属性名和方法名就冲突了

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-1 22:26:03 | 显示全部楼层
会的,你把self.start 改成self.begin
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-2 07:49:33 | 显示全部楼层
浅晓寒 发表于 2019-9-1 22:26
会的,你把self.start 改成self.begin

import time as t

class MyTimer:
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始...")

    #停止计时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时结束!")

   
    # 内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt += str(self.lasted[index])

        print(self.prompt)

第一次运行这个,就没有报错,没有说属性名和方法名矛盾
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-2 21:25:08 | 显示全部楼层
SQ551 发表于 2019-9-2 07:49
import time as t

class MyTimer:

不可能报错,因为直接覆盖了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-3 08:09:35 | 显示全部楼层
永恒的蓝色梦想 发表于 2019-9-2 21:25
不可能报错,因为直接覆盖了


浅晓寒 发表于 2019-9-1 22:26
会的,你把self.start 改成self.begin

import time as t

class MyTimer:
    #开始计时
    def start(self):
        self.start = t.localtime()
        print("计时开始...")

    #停止计时
    def stop(self):
        self.stop = t.localtime()
        self._calc()
        print("计时结束!")

   
    # 内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = "总共运行了"
        for index in range(6):
            self.lasted.append(self.stop[index] - self.start[index])
            self.prompt += str(self.lasted[index])

        print(self.prompt)

运行
>>> t1 = MyTimer()
>>> t1.start()
计时开始...
>>> t1.stop()
总共运行了000008
计时结束!

是正常运行的,self.start    和方法start()都起作用了。
然后在开头加上
def __init__(self):
        self.prompt = "未开始计时!"
        self.lasted = []
        self.start = 0
        self.stop = 0
运行:
>>> t1 = MyTimer()
>>> t1
未开始计时!
>>> t1.start()
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    t1.start()
TypeError: 'int' object is not callable
这个时候就报错了。
那么   __init__(self) 里的 self.start 属性和 start()方法里的self.start属性,请问区别是什么,都是类的属性呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-3 12:42:31 | 显示全部楼层
SQ551 发表于 2019-9-3 08:09
浅晓寒 发表于 2019-9-1 22:26
会的,你把self.start 改成self.begin

这时候的start是int
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-4 08:38:03 | 显示全部楼层

没明白你说的,请问,   __init__(self) 里的 self.start 属性和 start()方法里的self.start属性,请问区别是什么,都是类的属性呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-4 19:01:46 | 显示全部楼层
SQ551 发表于 2019-9-4 08:38
没明白你说的,请问,   __init__(self) 里的 self.start 属性和 start()方法里的self.start属性,请问区 ...

我不明白你到底在问什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-4 19:03:12 | 显示全部楼层
SQ551 发表于 2019-9-4 08:38
没明白你说的,请问,   __init__(self) 里的 self.start 属性和 start()方法里的self.start属性,请问区 ...

没有区别,但是python一切皆对象,start()方法被start属性覆盖了,所以会
  1. TypeError: 'int' object is not callable
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-18 00:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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