擅长:python、mysql、java
<p>它不是Python语言的特性,而是库上的一个方法。您可以在这里看到定义:</p>
<p><a href="https://github.com/hit9/CURD.py/blob/master/CURD.py#L251">https://github.com/hit9/CURD.py/blob/master/CURD.py#L251</a></p>
<pre><code>class Field(Leaf):
"""
Field object.
Field examples: User.name, User.age ..
"""
def __init__(self, is_primarykey=False, is_foreignkey=False):
self.is_primarykey = is_primarykey
self.is_foreignkey = is_foreignkey
# describe model's attr
def describe(self, name, model):
self.name = name
self.model = model
# fullname e.g. : User.id 's fullname is "user.id"
self.fullname = self.model.table_name + "." + self.name
# describe the attribute, reload its access control of writing, reading
setattr(model, name, FieldDescriptor(self))
</code></pre>
<p><code>setattr</code>设置对象的属性。因此,如果我调用<code>describe("field_name", myObject)</code>,它将把<code>myObject.field_name</code>设置为模型的描述。或者类似的事情。在</p>