为什么在使用`functools.partial`调用时,python的`datetime.strptime`函数的行为方式不同?

时间:2019-06-24 23:57:17

标签: python datetime

这是我面临的错误的一个示例:

In [1]: from functools import partial                                                                                             

In [2]: from datetime import datetime                                                                                             

In [3]: datetime.strptime("2/3/2016", "%m/%d/%Y")                                                                                 
Out[3]: datetime.datetime(2016, 2, 3, 0, 0)

In [4]: partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")                                                                        
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-d803aff4879c> in <module>
----> 1 partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")

~/miniconda3/envs/ROS/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    563     """Return a class cls instance based on the input string and the
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]
    567     args = tt[:6] + (fraction,)

~/miniconda3/envs/ROS/lib/python3.6/_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %

ValueError: time data '%m/%d/%Y' does not match format '2/3/2016'

如何使用datetime.strptime使partial正常运行?我使用partial或使用datetime.strptime的方式是否有问题?

2 个答案:

答案 0 :(得分:4)

您首先通过strptime将格式(应该是第二个参数)传递给partial,然后传递日期字符串(应该是第一个参数) ,导致错误。

您不能像这样将datetime.strptimepartial一起使用,因为它没有任何关键字参数。相反,您可以使用常规函数:

In [246]: def get_dt(string): 
     ...:     return datetime.strptime(string, "%m/%d/%Y") 
     ...:                                                                                                                                                                                                   

In [247]: get_dt("2/3/2016")                                                                                                                                                                                
Out[247]: datetime.datetime(2016, 2, 3, 0, 0)

答案 1 :(得分:0)

确保接受beemayl的回答。我只需要添加一些可能有用的东西。

确实,您看到的问题是对partial的“参数”的排序:

>>> from functools import partial  
>>> from datetime import datetime  
>>> datetime.strptime("2/3/2016", "%m/%d/%Y") 
datetime.datetime(2016, 2, 3, 0, 0)
>>> partial(datetime.strptime, "%m/%d/%Y")("2/3/2016")
Traceback (most recent call last):
    ValueError: time data '%m/%d/%Y' does not match format '2/3/2016'

实际上,如果您反转参数,则partial可以起作用:

>>> partial(datetime.strptime, "2/3/2016")("%m/%d/%Y")
datetime.datetime(2016, 2, 3, 0, 0)

但这当然不是您想要的。

因此,您可能会认为您可以在这里利用kwargs ...实际上,如果您查看文档,它会说:

  

classmethod datetime.strptime(date_string,format)

     

返回与date_string对应的日期时间,并根据格式进行解析。

所以我们尝试一下:

>>> partial(datetime.strptime, format="%m/%d/%Y")("2/3/2016")
Traceback (most recent call last):
    TypeError: strptime() takes no keyword arguments

不带关键字参数!什么?!是的,大多数“来自C”的函数实际上都没有!默认情况下,除非您使用the cool new feature starting in Python 3.8 that lets you prohibit them,否则您在Python中编写的函数始终会产生扭曲。

有趣的是,如果您拥有自己的 strptime:

>>> def my_strptime(date_string, format):
...     return datetime.strptime(date_string, format)
... 

然后您可以做您想做的事!

>>> partial(my_strptime, format="%m/%d/%Y")("2/3/2016")
datetime.datetime(2016, 2, 3, 0, 0)

只要您使用kwarg format

相关问题