Django,Python和类变量 [英] Django, Python, and Class Variables

查看:44
本文介绍了Django,Python和类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习Django的同时学习Python.我熟悉许多其他语言.

I'm simultaneously learning Python while picking up Django. I'm familiar with many other languages.

在以下代码段中, x 是类 Foo 的类变量.

In the following code snippet, x is a class variable of class Foo.

class Foo(object):
    x = 9000

鉴于先前的声明,以下内容可以正常工作.

Given the previous declaration, the following works fine.

打印Foo.x

Django框架允许您通过定义Python类来创建模型.它使字段来自Python类中的不同类变量.

The Django framework lets you create your model by defining Python classes. It makes fields out of the different class variables in your Python classes.

class Question(models.Model):
    question_text = models.CharField(max_length=200)

为什么下面的代码片段:

Why does the following code snippet:

#!/usr/bin/env
import os, django
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
django.setup()
from polls.models import Question, Choice
print Question.question_text

引发以下错误:

AttributeError: type object 'Question' has no attribute 'question_text'

据我了解,我的 Question 类具有定义的单个静态成员: Question.question_text .

As far as I'm understanding everything my Question class has a single static member defined: Question.question_text.

推荐答案

魔术.

不,真的.

Python类不是固定的结构,就像在C ++中一样.它们本身就是对象-其他类型的实例:

Python classes aren't set-in-stone structure, like they are in C++. They are, themselves, just objects — instances of another type:

class Foo(object):
    pass

print(type(Foo))  # <class 'type'>

您甚至可以通过调用 type 来创建类,就像创建其他任何对象一样.这个:

You can even make a class like you'd make any other object, by calling type. This:

class Bar(object):
    a = 1
    b = 2

实际上是(或多或少)语法糖:

Is really (more or less) syntactic sugar for this:

Bar = type('Bar', (object,), {'a': 1, 'b': 2})

type 接受新类的名称,其超类的列表以及该类所有属性的字典,然后吐出一个新类.

type takes the name of your new class, a list of its superclasses, and a dict of all the attributes of the class, and spits out a new class.

但是,由于 type 只是一个类,因此可以对其进行子类化并赋予其不同的行为.这就是Django所做的:创建一个 type 的子类,该子类与传递给它的属性的用法有所不同.

But, because type is just a class like any other, it's possible to subclass it and give it different behavior. And this is what Django has done: it's created a subclass of type that does something different with the dict of attributes you pass to it.

您不会在自己的代码中直接看到这种情况,但是如果检查 type(models.Model),您会发现它的类型不是 type ,但特定于Django.它的名称中可能带有元",因为它被称为 metaclass :类的类.

You don't see this happening directly in your own code, but if you check type(models.Model), you'll find out its type is not type, but something specific to Django. It probably has "meta" in the name, because it's called a metaclass: the class of a class.

这是在Python中制作声明式"库的相当普遍的模式,其中类的属性实际上定义了某种结构.您可以在表单验证(wtforms),模式验证(colander),其他ORM(sqlalchemy)甚至stdlib枚举模块中看到相同的内容.

This is a fairly common pattern for making "declarative" libraries in Python, where the attributes of a class actually define some kind of structure. You can see the same thing in form validation (wtforms), schema validation (colander), other ORMs (sqlalchemy), and even the stdlib enum module.

这篇关于Django,Python和类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆