python - callable as instancemethod? -


Text after "

Let's say we have a metaclass CallableWrappingMeta which runs a new class, body of wrapping Let's have got

  import type class CallableWrappingMeta (type): def __new __ (MCLs, names, bases, cls_dict): for Kashmir, V in a class,  InstanceMethodWrapper < / Code> with its methods cls_dict.iteritems (): ifinstance (V, types.FunctionType): cls_dict [k] = InstanceMethodWrapper (v) return type .__ new__ (MCLs, names, locations, cls_dict) class InstanceMethodWrapper ( Object): def __init __ (self, method): print "InstanceMeth OdWrapper .__ call __ (% s, *% r, **% r) "% (self, arg, kw) return self: self.method = method def __ (self, * args, ** kw) __call. Method (* Args, ** kilowatts) Square Bar (object): __metaclass__ = Callable Validating Meta def __init __ (self): Print 'Bar!'  

Our dummy cover will just give you some special attention when it comes to logic. The method, example-object receiver is not passed, because even though InstanceMethodWrapper is collabel, it is not considered as a function for the purpose of converting to an instance method during class build ( Is done with our metacalls).

Using a Possible Solution to Conduct Wrap Decorators - Instead of a Class Decorator - This Function Will Become an Example Method But in the real world, INSTUS-DULUWAPPER is more complex : It provides an API and publishes method-call events.

I have also tried to end some dead-ends. The sub-coding types.MethodType and types.UnboundMethodType did not go anywhere. A little introspection, and it appears that they bend at the type , so I tried to use both as a meta-class, but no luck is there, it can be a case. They have special demands in the form of MetaCLASS, but it seems we are in the non-documented area at this point.

Any thoughts?

After the

you just improve the code get a __ (which can perfectly enter the self with the class ) - that is, make that class in an descriptor type so that its Examples are description items See for background and details.

BTW, if you are on Python 2.6 or better then consider using a class-decorator instead of that metalex - we added the class decorator exactly because it used a lot of meta-tags such as For decoration purposes, and the decorators are really very easy to use.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -