![]() |
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 ''' |
![]() |
Eris · 纯虚拟成员有什么优势吗(除了他们可能防止的人为错误)? 2 年前 |
![]() |
AJA SMBAT · 我在获取列表而不是绑定方法时遇到问题 2 年前 |
![]() |
KiraHoneybee · 具有构造函数参数的模板化类 2 年前 |
![]() |
amirreza870 · Python OOP-更改类文本 2 年前 |
![]() |
Rocket Procd · 获取活动类实例的数量 2 年前 |
![]() |
cred · 为什么我请求的变量在从另一个类调用时显示为0.0? 2 年前 |
![]() |
Oliver Guy · 一个类的所有实例共享相同的值 2 年前 |