metaclass

Weird inheritance with metaclasses

若如初见. 提交于 2019-12-11 19:28:14
问题 I'm experiencing some really weird problems in Python when trying to inherit from a class with a metaclass. I have this: class NotifierMetaclass(type): def __new__(cls, name, bases, dct): attrs = ((name, value) for name, value in dct.items() if not name.startswith('__')) def wrap_method(meth): return instance_wrapper()(meth) # instance_wrapper is a decorator of my own def is_callable(value): return hasattr(value, '__call__') decorated_meth = dict( (name, value) if not is_callable(value) else

How to call object method for any object in my metaclass?

醉酒当歌 提交于 2019-12-11 19:21:26
问题 I have basically this object structure: TJSONStructure = class(TObject); TReqBase = class(TJSONStructure) private token: Int64; public procedure FillWithTemplateData; virtual; end; TReqLogin = class(TReqBase) private username, password: String; module : Integer; public procedure FillWithTemplateData; override; end; procedure TReqBase.FillWithTemplateData; begin token := ...; end; procedure TReqLogin.FillWithTemplateData; begin inherited; username := ...; password := ...; module := ...; end;

How to keep track of keywords passed to metaclass?

橙三吉。 提交于 2019-12-11 17:49:49
问题 I have a metaclass that accepts keyword arguments: class M(type): def __new__(cls, *args, **kwargs): print(*kwargs.items()) super().__new__(cls, *args) It works as expected: class A(metaclass=M, test='X'): pass results in an object A and the printout ('test', 'X') I would like to make a copy of A using something like this: def copy_class(cls, name=None): if name is None: name = cls.__name__ return type(cls)(name, cls.__bases__, dict(cls.__dict__)) A_copy = copy_class(A, 'A_copy') However, the

python apply decorator to every method in a class without inspect

守給你的承諾、 提交于 2019-12-11 17:48:03
问题 Slightly modifying the answer from Applying python decorators to methods in a class, it is possible to apply a decorator to every method in a class. Is there any way to do this without the inspect module? I've been trying to accomplish this using metaclasses and modifying __getattribute__ but I keep getting infinite recursion. From How is the __getattribute__ method used?, this can be fixed in normal classes using object.__getattribute__(self, name). Is there anything equivalent for

IPython representation of classes

你离开我真会死。 提交于 2019-12-11 04:32:39
问题 I was trying IPython with a module I created and it does not show the actual representation of class objects. Instead it shows something like TheClass.__module__ + '.' + TheClass.__name__ I heavily use metaclasses in this module and I have really meaningful class representations that should be shown to the user. Is there an IPython specific method I can change to make the right representation available instead of this namespace thingy that is quite useless in this application? Or, if that's

Python: Modifying passed arguments before class instance initialization

ⅰ亾dé卋堺 提交于 2019-12-11 03:52:42
问题 I am experimenting with ways to implement a simplified Term Rewriting System (TRS)/Symbolic Algebra System in Python. For this I would really like to be able to be able to intercept and modify the operands in particular cases during the class instance instantiation process. The solution I came up with, was to create a metaclass that modifies the typical call behavior of a class object (of type 'type'). class Preprocess(type): """ Operation argument preprocessing Metaclass. Classes using this

Is it possible to make the output of `type` return a different class?

為{幸葍}努か 提交于 2019-12-11 02:20:22
问题 So disclaimer: this question has piqued my curiosity a bit, and I'm asking this for purely educational purposes. More of a challenge for the Python gurus here I suppose! Is it possible to make the output of type(foo) return a different value than the actual instance class? i.e. can it pose as an imposter and pass a check such as type(Foo()) is Bar ? @juanpa.arrivillaga made a suggestion of manually re-assigning __class__ on the instance, but that has the effect of changing how all other

Abstract Enum Class using ABCMeta and EnumMeta [duplicate]

风流意气都作罢 提交于 2019-12-10 20:29:50
问题 This question already has answers here : Create an abstract Enum class (2 answers) Closed 7 months ago . Simple Example The goal is to create an abstract enum class through a metaclass deriving from both abc.ABCMeta and enum.EnumMeta . For example: import abc import enum class ABCEnumMeta(abc.ABCMeta, enum.EnumMeta): pass class A(abc.ABC): @abc.abstractmethod def foo(self): pass class B(A, enum.IntEnum, metaclass=ABCEnumMeta): X = 1 class C(A): pass Now, on Python3.7, this code will be

Given an instance of a Ruby object, how do I get its metaclass?

两盒软妹~` 提交于 2019-12-10 15:59:53
问题 Normally, I might get the metaclass for a particular instance of a Ruby object with something like this: class C def metaclass class << self; self; end end end # This is this instance's metaclass. C.new.metaclass => #<Class:#<C:0x01234567>> # Successive invocations will have different metaclasses, # since they're different instances. C.new.metaclass => #<Class:#<C:0x01233...>> C.new.metaclass => #<Class:#<C:0x01232...>> C.new.metaclass => #<Class:#<C:0x01231...>> Let's say I just want to know

Groovy: Execute Code transparent before and after any method is invoked

故事扮演 提交于 2019-12-10 15:39:17
问题 Let's say we have a groovy class with some methods (static or not static). What i want to do is executing some code before and after every method of this class is invoked without touchung the class at all and without dynamically manipulating the code inside of each method. What i tried using groovy metaClass; getting all methods of the metaClass and then dynamically replacing the every method with a wrapping method, containing some code and in the middle invoking the old method. Problem is, i