inheritance - Python: how to inherite and override -


Consider this situation:

I type the object type a The function is f Ie:

  Class A: Def F (self): 'FH DHH in print' (self): print 'HH'  

and I get an example of this class, but I want to override the f function, but the functionality of the rest of A Save So whatever I was thinking was something:

  square b (a): def __init __ (self, a): # some here .... def f (self): print 'B-> F ( 

and its use will be:

  def main (A): B = B (A) BF () # print "B- What is the type of copy constructor I want to do that is a parent of the current class ( a ) And gives an example of this class ( B ). 

How do you do this? How do the __init __ method appear?

Note : This post has been edited by the original poster to include the changes given below, due to which some suggestions seem to be inaccurate or inaccurate.

How do you subsection B "class a Based on one of the "only depends on the base how the state lives, if any, and how do you get the best in that situation and copy it? In your example, the examples of A are stateless, so you do not need to do this in B 's ' __init __ '. In a more specific example, say:

  class A (object): def __init __ (self): self._x = 23 self._y = 45 def f (self): print ' F, ',' self._x def h (self): print 'h,', self._y  

state will have two frequency characteristics in _x and _y , these are the ones that you need to copy:

  square b (a): def __init __ (self, a): self._x = A _x self._y = a._y def f (self): print 'b-> gt; F, ', self._x  

This is the most common and common viewpoint, where the subclass accepts and applies its state-dependency directly to the superclass - it is very straightforward and Linear is

You typically search for aspects of the A for the a of '__init __' , Because the most common, direct PORN code installs the example state in the initial form (attributes probably can be added and deleted later) May receive, or even from code outside the class body, but it is normal and not generally suitable).

It is possible to add a little touch of "magic" (for intuition-based programming), for example: ..

  class B1 (A): def __init __ (self , A): Try: A = a .__ getstate (__) Except AttributeError: try s = a .__ dict__: self. Except attributeError setstate __ (s): self.deccine.d__ Update (s)  

is a special method that can define classes - if they do, then use it (like by pickling) for the serialization purpose "State of your instances Received "(otherwise, the example of the __oncounter __ is considered to be the" state "of the example) it can change one word (in that case .update call update self 's state), but also anything back If the class also accepts it (therefore the code tries to route it before falling back to the possibility of updating), note that in the case of this usage, either the two special The methods will be inherited from the A - I will not define / override them in B (unless it is too subtle to achieve that course course - -).

Am I supposed to use these four lines of "magic" in return for the first time suggested simple task? Most, no - simplicity is better, but if A is subject to either special or external state, then this solution can be more powerful and normal (you are buying only by accepting its complexity). So, you have to know that what matters in the latter case (and then "go for big guns" of special state-related methods), or if the A and its examples are "very common vanilla" In that situation, I strongly recommend to choose simplicity and clarity instead.


Comments