چله تابستون 🔥 چله تخفیف استثنایی راکت، این فرصت دیگه پیش نمیاد!
فرصت محدود، تعداد محدودسلام خدمت همه
چجوری میشه یه متاکلاس مثل متاکلاس های جنگو ایجاد کرد؟
من خودم یه چیزی همینطوری نوشتم
class Serializer(type):
def __new__(cls, name, bases, attrs):
return type(name, bases, attrs)
class GlobalSerializer(metaclass=Serializer):
pass
class UserLoginSerializer(GlobalSerializer):
class Meta:
Model = User
Fields = fields = ['name', 'token', 'phone', 'username']
میخوام از اون کلاس Serializer به meta class اخر دست پیداکنم
میشه؟
اصلا روشش چجوریه؟
کد زیر یک مثال هست فکر کنم میتونه بهتون در درکش کمک کنه
def make_hook(f):
"""Decorator to turn 'foo' method into '__foo__'"""
f.is_hook = 1
return f
class MyType(type):
def __new__(mcls, name, bases, attrs):
if name.startswith('None'):
return None
# Go over attributes and see if they should be renamed.
newattrs = {}
for attrname, attrvalue in attrs.iteritems():
if getattr(attrvalue, 'is_hook', 0):
newattrs['__%s__' % attrname] = attrvalue
else:
newattrs[attrname] = attrvalue
return super(MyType, mcls).__new__(mcls, name, bases, newattrs)
def __init__(self, name, bases, attrs):
super(MyType, self).__init__(name, bases, attrs)
# classregistry.register(self, self.interfaces)
print "Would register class %s now." % self
def __add__(self, other):
class AutoClass(self, other):
pass
return AutoClass
# Alternatively, to autogenerate the classname as well as the class:
# return type(self.__name__ + other.__name__, (self, other), {})
def unregister(self):
# classregistry.unregister(self)
print "Would unregister class %s now." % self
class MyObject:
__metaclass__ = MyType
class NoneSample(MyObject):
pass
# Will print "NoneType None"
print type(NoneSample), repr(NoneSample)
class Example(MyObject):
def __init__(self, value):
self.value = value
@make_hook
def add(self, other):
return self.__class__(self.value + other.value)
# Will unregister the class
Example.unregister()
inst = Example(10)
# Will fail with an AttributeError
#inst.unregister()
print inst + inst
class Sibling(MyObject):
pass
ExampleSibling = Example + Sibling
# ExampleSibling is now a subclass of both Example and Sibling (with no
# content of its own) although it will believe it's called 'AutoClass'
print ExampleSibling
print ExampleSibling.__mro__
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟