1

I am getting an exception when I run the following code which I can not explain:

import datetime
datetime.datetime.strptime("2018-04-02", format = "%Y-%m-%d")

TypeError: strptime() takes no keyword arguments

1 Answer 1

7

Remove the format = keyword, the second argument is positional only:

>>> import datetime
>>> datetime.datetime.strptime("2018-04-02", "%Y-%m-%d")
datetime.datetime(2018, 4, 2, 0, 0)
Sign up to request clarification or add additional context in comments.

2 Comments

Why not? How do they make this distinction? I look at the function signature and I find : def strptime(cls, date_string, format): How come I can't name the keyword arguments here?
@Aetos: because the method is defined in C code, and most native functions and methods are positional-only. Generally speaking, for code written in C, supporting arguments both as positional and as keyword arguments carries a performance penalty so such support is rarely added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.