Inheritance of from_ in Python -


Edit: There was some confusion, but I want to ask a general question about object-oriented design in Python .

  class datamap: pass        

P> Now consider sub-squared which allows you to create a histogram from a list of data:

  class histogram (datamap): def __init __ (auto, list_of_false): # 1. Suitable Super ...) Call it if necessary: ​​Make a map of the values ​​counted in self-pass. Now consider a class that creates a suspicious prospect table, which is the  histogram .  
  Class ProbabilityMaster (Datamap): Pass   

What's the best way What is Maybe Histogram can be created from or list of values?

I have "grown up" programming in C ++, and in this case I use one overloaded constructor in Python I have thought about doing this:

    < Li> Constructor takes many arguments (all but none == none)
  • I define from_Histogram and in_list methods

    In the second case (Which I believe is better), allow the from_list method to use the shared code from What is the best way to histogram? Constructor? A probability value table is similar to the table of almost histograms , but it is extended so that the sum of all values ​​is 1.0.

    If you have come across a similar problem, please share your expertise! To start with

    , if you think that you @ If you want staticmethod , you do not always do this function either, this function is not a part of the class, in which case it should be just a free function, or it is part of the class, but not tied to an example , And it should be a @ classmethod . Your nominated producer is a good candidate for a @ classmate .

    Also keep in mind that you can go through a .__ init __ to b super More inheritance than you can cut off evil.

      class A: def __init __ (self, data): self.values_to_counts = {} in the val data: if value self.values_to_counts: self.values_to_counts [val] + = 1 other: self .values_to_counts [val] = 1_to_ values_to_counts (CLS, values_to_counts): self = CLS ([]) self.values_to_counts = Values_to_counts Self class B (A): DF __int __ (self, data, parameter) returns: Super (b, self) .__ init __ (data) self.parameter = parameter def print_parameter (self): print self.parameter   

    In this case, you do not need a B.from_values_to_counts , it inherits from a and this is a form of B Deer will come back , that's because it was called

    b needs to do more complex initialization, you can, When you use it with examples, a classmethod is actually a in the form of super () , which looks very similar to the way it will be. Examplemethod is not much more complex than , where im_self attribute is assigned to the class itself.

      class A: def __init __ (self, data): data for val in self.values_to_counts = {}: if in self.values_to_counts: self.values_to_counts [val] + = 1 Other: self.values_to_counts [VAL] = 1 @classmethod DEF from_values_to_counts (CLS, values_to_counts): self = cls ([]) self.values_to_counts = values_to_counts return self class B (A): def __init __ (self, data, parameters) : Super (b, self) .__ init __ (data) self.parameter = parameter def print_parameter (self): print self.parameter @classmethod DEF from_values_to_counts (CLS, values_to_counts): self = super (b, cls) .from_values_to_counts Values_to_counts) do_more_initialization Self)     

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -