Python3.xx:类中的类
我需要python脚本的帮助。如何通过Date类访问我的时钟功能Python3.xx:类中的类,python,python-3.x,class,subclassing,Python,Python 3.x,Class,Subclassing,我需要python脚本的帮助。如何通过Date类访问我的时钟功能 from datetime import date from datetime import datetime class Date(object): def date_today(self): now = date.today() print (now) class Time(Date): pass def clock(self): hr = datet
from datetime import date
from datetime import datetime
class Date(object):
def date_today(self):
now = date.today()
print (now)
class Time(Date):
pass
def clock(self):
hr = datetime.now()
hr_now = hr.hour
print (hr_now)
cr_date = Date()
print (cr_date.date_today())
print (cr_date.date_today.clock())
我得到一个错误-->AttributeError:“function”对象没有属性“clock”。此错误的原因是什么?您还可以在时间类中添加分钟、秒和其他相关函数。我希望这会有帮助
from datetime import date
from datetime import datetime
class Time():
def clock(self):
hr = datetime.now()
hr_now = hr.hour
return hr_now
class Date():
def __init__(self):
self.time = Time()
def date_today(self):
now = date.today()
return now
def clock(self):
return self.time.clock()
cr_date = Date()
print(cr_date.date_today())
print(cr_date.clock())
是的,您的日期类没有时钟()函数。我有两个类,日期类和时间类。我只想通过日期类访问时间类,您有什么建议吗?非常感谢Harun先生。这对我很有帮助。这个脚本cru date.date\u today.clock()的意思是什么?(在我之前的脚本中)您不必这样调用。在您的问题中,date_today()是一个函数,并且没有clock()函数作为属性,因此它会引发错误。