【发布时间】:2021-11-16 05:22:44
【问题描述】:
我是 python 语言的新手。 而且我的问题肯定是一个幼稚的问题,并且涉及 python 语法。
我正处于从理论到实践的阶段。
这是一个我想翻译成 python 语言的类(一个打字稿)。
class Category {
id: number;
type: 'shop'|'blog';
name: string;
slug: string;
path: string;
image: string|null;
items: number;
customFields: CustomFields;
parents?: Category[]|null;
children?: Category[]|null;
}
由于 python 是无类型语言,我对如何翻译有疑问:
- 可选属性:'?'
- 关联类:customFields: CustomFields;
- 关联类(自关联)和可为空的数组:children?: Category[]|null;
到目前为止,我一直在使用类型化语言,而什么都不写会破坏我的习惯。
会是这样吗(它是 django.db 迁移的模型):
>from django.db import models
>>class Category(models.Model):
>>> id = models.AutoField(primary_key=True)
>>> type: 'shop'|'blog'
>>> name = models.CharField(max_length=100)
>>> slug = models.CharField(max_length=100)
>>> path = models.CharField(max_length=250)
然后……?
您能否提供一些教程、文档、示例,让您在实践中学习python? 谢谢大家!
【问题讨论】:
-
TypeScript 是 ECMAScript 的正确超集。您可以直接删除所有类型,然后在 ECMAScript(一种动态类型语言)中拥有完全相同的内容。
标签: python typescript model definition