简体   繁体   English

python collections 如何接受多种数据类型?

[英]How does python collections accept multiple data types?

The most popular python version is CPython, written in C.最流行的 python 版本是 CPython,用 C 编写。 What i want to know is how is it possible to write a python collection using C when C arrays can only store on type of data at the same time? What i want to know is how is it possible to write a python collection using C when C arrays can only store on type of data at the same time?

This is not how python does it in C, but I've written a small interpreted language in Java (which also only allows arrays/lists with 1 data type) and implemented mixed type lists.这不是 python 在 C 中的做法,但我在 Java 中编写了一种小型解释语言(它也只允许使用 1 种数据类型的混合类型的数组/列表)。 I had a Value interface and a class for each type of value and those classes implemented the Value interface.对于每种类型的值,我都有一个 Value 接口和一个 class ,这些类实现了 Value 接口。 I had FunctionValue class, a StringValue class, a BooleanValue class, and a ListValue class, all of which implemented the value interface.我有 FunctionValue class、StringValue class、BooleanValue class 和 ListValue ZA2F2ED4F8EBC40619 的值接口。 The ListValue class has a field of type List<Value> which contains the list's elements. ListValue class 有一个List<Value>类型的字段,其中包含列表的元素。 All methods on the Value interface and its implementing classes which do stuff like numeric addition, string appending, list access, function calling, etc. initially take in Value objects and do different things based on which actual kind of Value it is. Value 接口及其实现类上的所有方法,如数字加法、字符串附加、列表访问、function 调用等,最初都接受 Value 对象,并根据它的实际类型做不同的事情。

You could do something similar in C, albeit at a lower level since it doesn't have interfaces and classes to help you manage your types.您可以在 C 中执行类似的操作,尽管级别较低,因为它没有帮助您管理类型的接口和类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM