![]() |
1
3
这在Python中根本不受支持。Django使用了元类来处理它。请看这个问题: How does Django Know the Order to Render Form Fields?
(小结:看
|
![]() |
2
5
Python 2.7和3.x定义了
您可以定义一个元类,它使用
从Python3.1语言参考第3.3.3节数据模型-自定义类创建:
不幸的是,Python2.7语言引用中的等效部分3.4.3没有提到能够替换类名称空间dict,也没有提到
|
![]() |
3
2
有一种方法比Django的方法更普遍,
我说它比django的方法更通用,因为您不需要将属性作为特定类的实例,任何值都可以。它也比
这种方法的主要缺点是,除了它有点难看之外,它不会与继承一起工作。不难得到
|
![]() |
4
1
你可以用
编辑:最可靠的方法是定义
如果没有Django的字段类技术,Python 2中就不能隐式地拥有顺序,因为Python只是不跟踪该信息;当调用元类时,它就会丢失。 |
|
5
0
下面让我捕获方法或struct类型的属性的顺序。我没有试过用内置类型。然后你可以在周围建一个步行者 儿童 class ordered_type(type): __ordernate__ = 0 def __new__(meta, name, baseclasses, dct): new = super(Type, meta).__new__(meta, name, baseclasses, dct) new.__ordernate__ = Type.__ordernate__ Type.__ordernate__ += 1 def key(x): from inspect import ismethod if ismethod(x): return x.im_func.__code__.co_firstlineno elif isinstance(x, type): return x.__ordernate__ + 1000000 new.__children__ = sorted( [getattr(new, x) for x in dir(new) if not x.startswith('__')], key=key) class struct(object): __metaclass__ = ordered_type #use case class A(struct): '''Description of class A ''' def name(self): '''The name of instance of A ''' class B(struct): '''Description of class B ''' def name(self): '''The name of instance of B ''' |
![]() |
Eddiex045 · 比较两个文本文件,匹配项转到一个新文件 2 年前 |
![]() |
NOBUD · 最大堆插入函数实现C++ 2 年前 |
![]() |
riasc · 嵌套贴图结构创建空贴图 6 年前 |
![]() |
Akshay Barpute · cpp中的以下链表程序有什么问题? 6 年前 |
![]() |
Batwoman05 · C++中是否有具有类似函数的树集数据结构 6 年前 |