metaclass

What does Python's builtin __build_class__ do?

喜欢而已 提交于 2019-11-29 05:32:39
In Python 3.1, there is a new builtin function I don't know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal helper function used by the class statement. What does this function do? Why must it be in builtins if it's internal? What is the difference to the type(name, bases, dict) function? CodeJoust Compiling the PEP 3115 metaclass Guido van Rossum said: The PEP proposes that the class statement accepts keyword arguments, *args , and **kwds syntax as well as positional bases. This is a bit messy to compile and execute,

Metaclass multiple inheritance inconsistency

夙愿已清 提交于 2019-11-28 23:32:18
问题 Why is this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyList(list, MyMixin): pass okay, and works as expected: created <class '__main__.MyMixin'> created <class '__main__.MyList'> But this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyObject(object, MyMixin): pass Is not okay, and blows up thusly?: created <class '__main__.MyMixin'

Is there a way to set metaclass after the class definition?

拥有回忆 提交于 2019-11-28 23:30:29
In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses are used at the time the class is defined, so setting it explicitly after the class definition has no effect. This is what happens when I try to set metaclasses explicitly; >>> class MetaClass(type): def __new__(cls, name, bases, dct): dct["test_var"]=True return type.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): super(MetaClass, cls).__init__(name, bases, dct) >>> class A: __metaclass__=MetaClass >>> A.test_var True >>> class B: pass >>> B.__metaclass__=MetaClass >>> B.test_var

Where are methods defined at the ruby top level?

瘦欲@ 提交于 2019-11-28 23:25:34
问题 At the top level, method definition should result in private methods on Object , and tests seem to bear this out: def hello; "hello world"; end Object.private_instance_methods.include?(:hello) #=> true Object.new.send(:hello) #=> "hello world" However, the following also works at top level ( self.meta is the eigenclass of main ): self.meta.private_instance_methods(false).include?(:hello) #=> true It appears that the hello method is simultaneously defined on the eigenclass of main as well as

Python Metaclass : Understanding the 'with_metaclass()'

╄→尐↘猪︶ㄣ 提交于 2019-11-28 19:33:22
问题 I want to ask what the with_metaclass() call means in the definition of a class. E.g.: class Foo(with_metaclass(Cls1, Cls2)): Is it a special case where a class inherits from a metaclass? Is the new class a metaclass, too? 回答1: with_metaclass() is a utility class factory function provided by the six library to make it easier to develop code for both Python 2 and 3. It uses a little slight of hand (see below) with a temporary metaclass, to attach a metaclass to a regular class in a way that's

How to auto register a class when it's defined

穿精又带淫゛_ 提交于 2019-11-28 18:36:06
I want to have an instance of class registered when the class is defined. Ideally the code below would do the trick. registry = {} def register( cls ): registry[cls.__name__] = cls() #problem here return cls @register class MyClass( Base ): def __init__(self): super( MyClass, self ).__init__() Unfortunately, this code generates the error NameError: global name 'MyClass' is not defined . What's going on is at the #problem here line I'm trying to instantiate a MyClass but the decorator hasn't returned yet so it doesn't exist. Is the someway around this using metaclasses or something? dappawit

What are some (concrete) use-cases for metaclasses?

只谈情不闲聊 提交于 2019-11-28 15:01:51
I have a friend who likes to use metaclasses, and regularly offers them as a solution. I am of the mind that you almost never need to use metaclasses. Why? because I figure if you are doing something like that to a class, you should probably be doing it to an object. And a small redesign/refactor is in order. Being able to use metaclasses has caused a lot of people in a lot of places to use classes as some kind of second rate object, which just seems disastrous to me. Is programming to be replaced by meta-programming? The addition of class decorators has unfortunately made it even more

Use class method not instance method with the same name

别说谁变了你拦得住时间么 提交于 2019-11-28 14:21:36
I have the following snippet: class Meta(type): def __getattr__(self, name): pass class Klass(object): __metaclass__ = Meta def get(self, arg): pass Now, if I do: kls = Klass() kls.get('arg') everything works as expected (the instance method get is called). But if I do: Klass.get('arg') again the instance method is found and an exception is given, since it is treated as an unbound method. How can I make a call to Klass.get('arg') go through the __getattr__ defined in the metaclass? I need this because I want to proxy all methods called on a class to another object (this would be done in _

Intercept operator lookup on metaclass

荒凉一梦 提交于 2019-11-28 13:29:35
I have a class that need to make some magic with every operator, like __add__ , __sub__ and so on. Instead of creating each function in the class, I have a metaclass which defines every operator in the operator module. import operator class MetaFuncBuilder(type): def __init__(self, *args, **kw): super().__init__(*args, **kw) attr = '__{0}{1}__' for op in (x for x in dir(operator) if not x.startswith('__')): oper = getattr(operator, op) # ... I have my magic replacement functions here # `func` for `__operators__` and `__ioperators__` # and `rfunc` for `__roperators__` setattr(self, attr.format(

Provide __classcell__ example for Python 3.6 metaclass

强颜欢笑 提交于 2019-11-28 11:12:47
Per the 3.6.0 docs: CPython implementation detail : In CPython 3.6 and later, the __class__ cell is passed to the metaclass as a __classcell__ entry in the class namespace. If present, this must be propagated up to the type.__new__ call in order for the class to be initialized correctly. Failing to do so will result in a DeprecationWarning in Python 3.6, and a RuntimeWarning in the future. Can someone provide an example of doing this correctly? An example where it's actually needed? The warning is raised if you use the zero argument super super().__method__(args) that relies on __class__ being