是否可以在实例化时从类中调用方法?
我尝试了以下但不断得到
Base Class:
class A(object):
""" Parent class """
def __init__(self, **kwargs):
# query parameters
self.start_time = kwargs.get('start_time', None)
def set_query_starttime(self, str_time):
""" sets query start time """
if self.verbosity:
print("I: Set query start time parameter")
if not str_time:
return
utc_start_time = self._format_time(str_time)
try:
self.query.SetStartTime(utc_start_time.tm_year,
utc_start_time.tm_mon,
utc_start_time.tm_mday,
utc_start_time.tm_hour,
utc_start_time.tm_min,
utc_start_time.tm_sec)
except pythoncom.com_error as error:
e = format_com_message("Failed to format date")
raise Error(e)
我想实例化基类并在实例化类时调用 set_query_starttime 方法:
myStartTime = "Sun Nov 03 20:00:00 2013"
myEndTime = "Sun Nov 03 21:00:00 2013"
test = A(start_time = A.set_query_starttime(myStartTime))
但不断得到: TypeError: unbound method set_query_starttime() must be called with A instance as first argument (got str instead)
我什至尝试将@staticmethod 添加到基类的 set_query_starttime 函数中,但仍然出现相同的错误。