Inherit from a built-in class
I would like to add some methods to the datetime.datetime object. It seems that we can only do that by inheriting from it and adding this new method. The problem is that this method need to update the day/month/year values of the base class and that i 开发者_如何学Ccan't call the base init method with the new parameters.
How can I do this?
You can call the base class __init__
method.
class Foo(datetime.datetime):
def __init__(self, argument):
datetime.datetime.__init__(self, argument)
The key point here is that you need to call the __init__
method explicitly and you need to manually supply the first self
argument that Python normally supplies for you.
Also, don't forget about the *
and **
calling techniques to catch arguments that you don't want to deal with manually but that you still want to be able to pass to the parent constructor.
精彩评论