metaclass

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

送分小仙女□ 提交于 2019-12-01 01:50:37
I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs , should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my attempts have been unsuccessful. Here's an SSCCE that should make isinstance return False in all cases but

How do I create a simple metaclass?

有些话、适合烂在心里 提交于 2019-12-01 00:13:55
I've been doing Python for some time now, and I've always somewhat understood the meaning of metaclasses, but I've never needed one. Now I think the best solution for my problem is a metaclass (correct me if there's a better way). What I'm trying to create is a system which automatically adds a class variable n and a list instances to each class of mine. Here's a simplified example of one class: class Foo: n = 0 instances = [] def __init__(self): self.index = Foo.n Foo.n += 1 Foo.instances.append(self) This structure should be implemented for 7 or 8 classes of mine, and I was thinking that a

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

本小妞迷上赌 提交于 2019-11-30 20:26:12
问题 I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs, should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my

How do I create a simple metaclass?

与世无争的帅哥 提交于 2019-11-30 18:49:55
问题 I've been doing Python for some time now, and I've always somewhat understood the meaning of metaclasses, but I've never needed one. Now I think the best solution for my problem is a metaclass (correct me if there's a better way). What I'm trying to create is a system which automatically adds a class variable n and a list instances to each class of mine. Here's a simplified example of one class: class Foo: n = 0 instances = [] def __init__(self): self.index = Foo.n Foo.n += 1 Foo.instances

How to define a Python metaclass with Boost.Python?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 11:46:39
The Python C API has the PyObject *PyType_Type object, which is equivalent to type in the interpreter. If I want to define a metaclass in C++, how can I set type as one of its bases in Boost.Python? Also, what other things should I take into consideration when defining a Python metaclass in C++? It'd be ideal if there was a Boost.Python solution to this. If not, a solution that uses the Python C API (or a combination of Boost and the C API) is good as well. Since my other classes are exposed with Boost, I'd rather leave SWIG as a last resort. Note: This is actually part of a bigger problem I'm

Ways to make a class immutable in Python

与世无争的帅哥 提交于 2019-11-30 11:20:00
I'm doing some distributed computing in which several machines communicate under the assumption that they all have identical versions of various classes. Thus, it seems to be good design to make these classes immutable; not in the sense that it must thwart a user with bad intentions, just immutable enough that it is never modified by accident. How would I go about this? For example, how would I implement a metaclass that makes the class using it immutable after it's definition? >>> class A(object): ... __metaclass__ = ImmutableMetaclass >>> A.something = SomethingElse # Don't want this >>> a =

Decorating a class method after @property

夙愿已清 提交于 2019-11-30 09:18:11
I want to wrap every method of various objects except __init__ using a decorator. class MyObject(object): def method(self): print "method called on %s" % str(self) @property def result(self): return "Some derived property" def my_decorator(func): def _wrapped(*args, **kwargs): print "Calling decorated function %s" % func return func(*args, **kwargs) return _wrapped class WrappedObject(object): def __init__(self, cls): for attr, item in cls.__dict__.items(): if attr != '__init__' and (callable(item) or isinstance(item, property)): setattr(cls, attr, my_decorator(item)) self._cls = cls def _

Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?

北慕城南 提交于 2019-11-30 08:44:22
I recently discovered metaclasses in python. Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritance works, etc. All of this becomes not only possible but simple. But in python, metaclasses are also plain classes. So, I started wondering if the abstraction could usefully go higher, and it seems to me that it can and that: a metaclass corresponds to or implements a role in a pattern (as

Use Groovy Category implicitly in all instance methods of class

帅比萌擦擦* 提交于 2019-11-30 08:43:19
I have simple Groovy category class which adds method to String instances: final class SampleCategory { static String withBraces(String self) { "($self)" } } I want to use this category in my unit tests (for example). It looks like this: class MyTest { @Test void shouldDoThis() { use (SampleCategory) { assert 'this'.withBraces() == '(this)' } } @Test void shouldDoThat() { use (SampleCategory) { assert 'that'.withBraces() == '(that)' } } } What I'd like to achieve, however, is ability to specify that category SampleCategory is used in scope of each and every instance method of MyTest so I don't

Dynamically define named classes in Ruby

一笑奈何 提交于 2019-11-30 08:02:56
I am writing an internal DSL in Ruby. For this, I need to programmatically create named classes and nested classes. What is the best way to do so? I recon that there are two ways to do so: Use Class.new to create an anonymous class, then use define_method to add methods to it, and finally call const_set to add them as named constants to some namespace. Use some sort of eval I've tested the first way and it worked, but being new to Ruby, I am not sure that putting classes as constants is the right way. Are there other, better ways? If not, which of the above is preferable? If you want to create