0

Guys if you know any other methods please tell about them. This function gives the error:

'str' object is not callable.

from datetime import date

def datetowords(date):

    day_, month_, year_ = int(date[:2]), int(date[3:5]), int(date[6:])
    print(date(day=day_, month=month_, year=year_).strftime('%A %d %B %Y'))


datetowords("02.11.2013")
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
  • 1
    Did you read [this](https://stackoverflow.com/questions/6039605/typeerror-str-object-is-not-callable-python) SO question and the answers? – Jeroen Heier Nov 24 '19 at 07:36
  • Does this answer your question? [What is variable shadowing?](https://stackoverflow.com/questions/53734399/what-is-variable-shadowing) – Alessandro Da Rugna Nov 24 '19 at 17:50

2 Answers2

4

Change your parameter name from date to gottendate or something else, because it conflicts with date from datetime module.

So when you call date(...) it calls the inner date variable, since python looks for the local scope first.

ori6151
  • 592
  • 5
  • 11
1

To add to @ori6151, you can simplify and speed up your code using the split() function and list comprehension.

For example:

from datetime import date

def datetowords(string):
    day_, month_, year_ = [int(i) for i in string.split('.')]
    print(date(day=day_, month=month_, year=year_).strftime('%A %d %B %Y'))

datetowords("02.11.2013")
lbragile
  • 7,549
  • 3
  • 27
  • 64