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?
Asked
Active
Viewed 140 times
0
-
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
-
1Why 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