【问题标题】:calling a method inside a class-Python在类 Python 中调用方法
【发布时间】:2017-11-02 04:40:57
【问题描述】:
class Time:

    def __init__(self,x,y,z):
        self.hour=x
        self.minute=y
        self.second=z

    def __str__(self):
        return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

    def time_to_int(time):
        minutes=time.hour*60+time.minute
        seconds=minutes*60+time.second
        return seconds

    def int_to_time(seconds):
        time=Time()
        minutes,time.second=divmod(seconds,60)
        time.hour,time.minute=divmod(minutes,60)
        return time

    def add_time(t1,t2):
        seconds=time_to_int(t1)+time_to_int(t2)
        return int_to_time(seconds)

start=Time(9,45,00)
running=Time(1,35,00)
done=add_time(start,running)
print(done)

我是 python 新手,最近一直在做一些练习。我遇到了一个问题,我已经编写了相同的代码。但我反复收到错误:“add_time 未定义”。我尝试定义一个 main() 方法,但它没有打印任何内容。请帮忙。

【问题讨论】:

  • 请修正缩进。 time_to_int 等应该是独立函数还是类的方法?如果是前者,则它们需要在类定义之外消隐。

标签: python class methods


【解决方案1】:

您还没有为上述类创建对象。

类中的任何函数/方法只能由该类的对象访问。有关面向对象编程基础的更多信息,请查看this页面。

与此同时,请按以下方式定义您的类:

class Time:

def __init__(self,x=None,y=None,z=None):
    self.hour=x
    self.minute=y
    self.second=z

def __str__(self):
    return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

def time_to_int(time):
    minutes=time.hour*60+time.minute
    seconds=minutes*60+time.second
    return seconds

def int_to_time(seconds):
    time=Time()
    minutes,time.second=divmod(seconds,60)
    time.hour,time.minute=divmod(minutes,60)
    return time

def add_time(t1,t2):
    seconds=time_to_int(t1)+time_to_int(t2)
    return int_to_time(seconds)

在类块之外,写下以下几行:

TimeObject = Time()
start=Time(9,45,00)
running=Time(1,35,00)
TimeObject.add_time(start,running)
print "done"

然而,我建议您在类外部编写 add_time 函数,因为您将对象作为参数传递给同一类中的函数,这在面向对象编程中被认为是一种糟糕的设计。 希望能帮助到你。干杯!

【讨论】:

  • 一个问题。现在根据您的代码,所有方法都是独立的?对吗?
  • 没有。 start 和 running 是您上课时间的对象。要访问 Time 类的 add_time 方法,您需要另一个对象,因此我创建了一个 TimeObject 只是为了访问 add_time 方法。不过我建议你让 add_time 方法独立,其余的可以在类中。
  • 这里你已经创建了一个 TimeObject 但它必须被初始化,否则会报错。
  • 对不起,我错过了,现在我已经编辑了,所以我可以初始化它而无需传递任何参数。
  • 谢谢@crazyglasses。我会支持你的回答,但我的声誉低于 15。
【解决方案2】:

为我工作:

class C:
  def f(a, b):
    return a + b
  x = f(1,2)

print(C.x)

但你不应该做这样的事情。当类“创建”时,类级别的代码正在执行,通常您需要静态方法或类方法(用@staticmethod@classmethod 装饰)并在某些函数/实例化类中执行代码。如果这是简单脚本,您也可以在顶层(模块)级别执行它。您的 sn-p 是“不好的做法”:类级别(我说的是缩进)是用于声明,而不是用于执行某些事情。在类级别上执行类似于 C 宏的代码是正常的:例如,调用装饰器,转换一些方法/属性/等 - 静态的东西是“纯”函数!

【讨论】:

    【解决方案3】:

    只要您在构造函数中指定 3 个参数,这对我来说就可以了

    def int_to_time(seconds):
        time=Time(0,0,0) # just set your 3 positionals args here
        minutes,time.second=divmod(seconds,60)
        time.hour,time.minute=divmod(minutes,60)
        return time
    

    另一种避免它的方法可能是:

    class Time:
        def __init__(self,x=0,y=0,z=0):
            self.hour=x
            self.minute=y
            self.second=z
    

    如果您想将您的函数添加到您的类(例如time_to_intint_to_time 甚至add_time),那么您需要再缩进一层 4 个空格并在您的方法中添加 self参数

    【讨论】:

    • 谢谢!我知道了。我试图在不使用对象的情况下调用类方法。
    【解决方案4】:

    嗨,Mathers25,
    我解决了你的问题试试下面的代码以获得最佳输出,

    class TimeClass:
    
    def __init__(self,x,y,z):
        self.hour = x
        self.minute = y
        self.second = z
    
    def __str__(self):
        return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)
    
    def time_to_int(self,time):
    
        minutes = (time.hour * 60) + time.minute
        seconds = (minutes * 60) + time.second
        return seconds
    
    def int_to_time(self,seconds):
        time = TimeClass(0,0,0)
            minutes,time.second=divmod(seconds,60)
            time.hour,time.minute=divmod(minutes,60)
            return time
    
    def add_time(self,t1,t2):
        seconds = self.time_to_int(t1) + self.time_to_int(t2)
        # Call method int_to_time() using self keyword.
        return self.int_to_time(seconds)
    
    
    # First time object create that time set value is 0 of hour,minute and second
    TimeObject = TimeClass(0,0,0)
    
    # After create second object
    start=TimeClass(9,45,00)
    
    # After create thired Object
    running=TimeClass(1,35,00)
    
    # Store the value which return by add_time() 
    done = TimeObject.add_time(start,running)
    
    # Display the value of done variable
    print(done)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多