metaclass

How to define a Python metaclass with Boost.Python?

做~自己de王妃 提交于 2019-11-29 17:15:57
问题 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

Ways to make a class immutable in Python

折月煮酒 提交于 2019-11-29 16:54:52
问题 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

Decorating a class method after @property

拈花ヽ惹草 提交于 2019-11-29 14:42:19
问题 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

Use Groovy Category implicitly in all instance methods of class

旧街凉风 提交于 2019-11-29 13:18:31
问题 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

How does Inheritance work in Ruby?

不羁岁月 提交于 2019-11-29 13:02:35
According to Dave Thomas in his talk about the Ruby Object Model , there are no "class methods" in Ruby. There is only difference between whether the receiver of the method is a "class object" or an "instance object". class Dave def InstaceMethod ### will be stored in the current class (Dave) puts "Hi" end class << self ### Creates an eigenclass, if not created before def say_hello puts "Hello" end end end By default, ancestors method doesn't show the metaclass: class Dave class << self def metaclass ### A way to show the hidden eigenclass class << self; self; end end end end p Dave.ancestors

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

删除回忆录丶 提交于 2019-11-29 11:59:37
问题 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

Dynamically define named classes in Ruby

亡梦爱人 提交于 2019-11-29 10:53:51
问题 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

Custom placeholder like None in python

只谈情不闲聊 提交于 2019-11-29 10:34:30
I'm using argspec in a function that takes another function or method as the argument, and returns a tuple like this: (("arg1", obj1), ("arg2", obj2), ...) This means that the first argument to the passed function is arg1 and it has a default value of obj1, and so on. Here's the rub : if it has no default value, I need a placeholder value to signify this. I can't use None, because then I can't distinguish between no default value and default value is None . Same for False, 0, -1, etc. I could make it a tuple with a single element, but then the code for checking it would be ugly, and I can't

Python3 Singleton metaclass method not working

♀尐吖头ヾ 提交于 2019-11-29 10:04:29
I saw a lot of methods of making a singleton in Python and I tried to use the metaclass implementation with Python 3.2 (Windows), but it doesn"t seem to return the same instance of my singleton class. class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] class MyClass(object): __metaclass__ = Singleton a = MyClass() b = MyClass() print(a is b) # False I use the decorator implementation now which is working, but I'm wondering what is wrong with this

How does a classmethod object work?

耗尽温柔 提交于 2019-11-29 09:23:05
问题 I'm having trouble to understand how a classmethod object works in Python, especially in the context of metaclasses and in __new__ . In my special case I would like to get the name of a classmethod member, when I iterate through the members that were given to __new__ . For normal methods the name is simply stored in a __name__ attribute, but for a classmethod there is apparently no such attribute. I don't even see how the classmethod is invoked, as there is no __call__ attribute either. Can