0

Is it possible to create an extension method in Python for datetime objects? I.e. I would like to create a date date = datetime.datetime(2022, 2, 2) and use my extension method which will do sth with that date by simply typing date.my_extension(args). Is it possible and if not, what is the best alternative for that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jason
  • 313
  • 2
  • 8
  • Did you read https://stackoverflow.com/questions/352537/extending-builtin-classes-in-python and https://stackoverflow.com/questions/192649/can-you-monkey-patch-methods-on-core-types-in-python already? – mkrieger1 Jun 10 '22 at 16:34
  • 1
    Why not just write a function and use it like `my_extension(date, args)`? What exactly are you trying to achieve? I sense an XY problem. – mkrieger1 Jun 10 '22 at 16:35
  • you make a subclass of it and add whatever you want to it – Copperfield Jun 10 '22 at 16:37

0 Answers0