metaclass

What's the correct way to implement a metaclass with a different signature than `type`?

别说谁变了你拦得住时间么 提交于 2019-12-10 14:49:39
问题 Say I want to implement a metaclass that should serve as a class factory. But unlike the type constructor, which takes 3 arguments, my metaclass should be callable without any arguments: Cls1 = MyMeta() Cls2 = MyMeta() ... For this purpose I defined a custom __new__ method with no parameters: class MyMeta(type): def __new__(cls): return super().__new__(cls, 'MyCls', (), {}) But the problem is that python automatically calls the __init__ method with the same arguments as the __new__ method, so

Python: Metaclass properties override class attributes, sometimes?

元气小坏坏 提交于 2019-12-10 14:34:50
问题 The result of the below code boggles me: class MyClass(type): @property def a(self): return 1 class MyObject(object): __metaclass__ = MyClass a = 2 print MyObject.a print object.__getattribute__(MyObject, 'a') print type.__getattribute__(MyObject, 'a') print MyObject.__dict__['a'] print MyObject().a I really expect this to just print 2 repeatedly, but it prints 1 1 1 2 2 . Is there a way this makes any intuitive sense? To clarify: I understand that this behavior is well documented (here,

Smalltalk superclass vs metaclass?

梦想与她 提交于 2019-12-10 13:05:59
问题 I new to OOP, but with a "procedural" background. I'm currently trying to get my head around OOP via GNU Smalltalk and Lovejoy's "Smalltalk: Getting The Message". I'm confused as to the the heck the metaclass and Metaclass class are, vs superclass. I can see the inheritance flow of superclass -> class -> subclass; but I don't see how/where metaclass fits in. TIA... 回答1: There is an excellent description in the free online book Pharo by Example, Chapter 13 (Classes and metaclasses). The things

Detect if class was defined declarative or functional - possible?

可紊 提交于 2019-12-10 12:53:19
问题 Here's a simple class created declaratively: class Person: def say_hello(self): print("hello") And here's a similar class, but it was defined by invoking the metaclass manually: def say_hello(self): print("sayolala") say_hello.__qualname__ = 'Person.say_hello' TalentedPerson = type('Person', (), {'say_hello': say_hello}) I'm interested to know whether they are indistinguishable. Is it possible to detect such a difference from the class object itself? >>> def was_defined_declaratively(cls): ..

Sphinx document module properties

*爱你&永不变心* 提交于 2019-12-09 15:08:06
问题 I have a module that should have a @property , I solved this by setting a class as the module. I got the idea from this answer: Lazy module variables--can it be done? I wanted this to be repeatable and easy to use so I made a metaclass for it. This works like a charm. The problem is that when using Sphinx to generate documentation properties don't get documented. Everything else is documented as expected. I have no idea how to fix this, maybe this is a problem with Sphinx? The module: import

When to inline definitions of metaclass in Python?

独自空忆成欢 提交于 2019-12-09 08:10:23
问题 Today I have come across a surprising definition of a metaclass in Python here, with the metaclass definition effectively inlined. The relevant part is class Plugin(object): class __metaclass__(type): def __init__(cls, name, bases, dict): type.__init__(name, bases, dict) registry.append((name, cls)) When does it make sense to use such an inline definition? Further Arguments: An argument one way would be that the created metaclass is not reusable elsewhere using this technique. A counter

Make Singleton class in Multiprocessing

非 Y 不嫁゛ 提交于 2019-12-08 08:48:23
问题 I create Singleton class using Metaclass , it working good in multithreadeds and create only one instance of MySingleton class but in multiprocessing, it creates always new instance import multiprocessing class SingletonType(type): # meta class for making a class singleton def __call__(cls, *args, **kwargs): try: return cls.__instance except AttributeError: cls.__instance = super(SingletonType, cls).__call__(*args, **kwargs) return cls.__instance class MySingleton(object): # singleton class _

How to create a metaclass that can give a class an array of instances and provide a “voodoo” instance that acts on all class instances?

ぃ、小莉子 提交于 2019-12-08 06:43:07
问题 I'm wondering how to create a metaclass in Python that can create other classes that: Store their instances in an array automatically Have a special instance, NonMetaClass.all , whose properties: When set, set all the class's instances with the same key to the same value (e.g., Foo.all.num = 3 makes all instances of Foo have a num of 3) When accessed (get), returns an array of all of the class's instances's key values (e.g., Foo.all.num returns [5, 3, 2] ) Cannot be deleted. When called (if

How to use the __subclasscheck__ magic method?

╄→гoц情女王★ 提交于 2019-12-08 06:05:18
问题 How can we make a class who lies about who he has subclassed? After reading the doc I've attempted this: >>> class AllYourBase(type): ... @classmethod ... def __subclasscheck__(cls, other): ... return True ... >>> class AllYour(object): ... __metaclass__ = AllYourBase Now, this class should report that all your base are belong to him. But it didn't work: >>> issubclass(AllYour, int) False Why not? 回答1: If you want AllYour to claim to be a subclass of every class, that isn't possible. _

Make Singleton class in Multiprocessing

北城余情 提交于 2019-12-08 04:14:22
I create Singleton class using Metaclass , it working good in multithreadeds and create only one instance of MySingleton class but in multiprocessing, it creates always new instance import multiprocessing class SingletonType(type): # meta class for making a class singleton def __call__(cls, *args, **kwargs): try: return cls.__instance except AttributeError: cls.__instance = super(SingletonType, cls).__call__(*args, **kwargs) return cls.__instance class MySingleton(object): # singleton class __metaclass__ = SingletonType def __init__(*args,**kwargs): print "init called" def task(): # create