python - Shortcut for super(type(self), self) -


I do this often when a method is overridden in a sub class:

 < Code> def method_x self): x = super (type (self), self). Mythoix () [some additional code] return x   

My question is: a short cut for the super (type (self), itself)

Do not do this: if super as the first code by using type (self) So, it must have been written not to take two arguments in the first place. You should not give an expression to the actual class here that can be converted to sub-sections of class.

The need for super which should be the current method definition class, as you are super list of bases to start searching for it.

Python 3 knows about it and behaves magically with super () on time compilation, but in Python 2.x this is just a simple task.

[edit to add my starting points] In fact, there is another less used method to use super () in Python 2. x You can use an unbound form and you can force it when you access it:

  & g T; & Gt; & Gt; Category A (object): def foo (self): print "afu" and gt; & Gt; & Gt; Category B (A): def foo (self): self.de super.foo () print "bfu" & gt; & Gt; & Gt; B._B__ super = super (b)> and gt; & Gt; Category C (A): def foo (self): auto.degree.foo () print "C.foo" & gt; & Gt; & Gt; C._C__ Super = Super (C)> & Gt; & Gt; Class D (C, B): Pass & gt; & Gt; & Gt; D._D__ super = super (d)> and gt; & Gt; D () Here's a key catch: You must use a different name to store every code   object, which is You can do this by using  self.de super , but you can not create an unbound  super  directly in class definition (because you can not name the class this Not yet in existence) and if you make it out, you will get it Certainly must manually obscure name to you to set the  __ Super  object to orbit Python 2.6 and later, you probably can wrap as class decorator.   

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -