简体   繁体   English

python如何在数组中具有不同的数据类型?

[英]How does python have different data types in an array?

Python can have a list with different data types in it ie [1,"two",3]. Python可以在其中具有不同数据类型的列表,即[1,“ two”,3]。 Python was implemented in c, so how would I create an array with different data types in it using c? Python是用c实现的,那么如何使用c在其中创建具有不同数据类型的数组?

So, I have no idea how it is implemented in Python, but in C there are ways to operate on generic data. 因此,我不知道如何在Python中实现它,但是在C语言中,有一些方法可以对通用数据进行操作。 in its most simple form: 最简单的形式:

void *array[size];

Now you have an array full of void* . 现在您有了一个充满void*的数组。 Each void* can point to anything. 每个void*都可以指向任何东西。 You would want some extra information as well to tell you the size of the thing it points to. 您还需要一些其他信息来告诉您它指向的内容的大小。

typedef struct {
    void *data;
    size_t size;
} elem;

You could represent the actual type if needed via a variety of methods. 如果需要,可以通过多种方法表示实际类型。 You can use unions to hold one of N types in the same variable. 您可以使用联合将N个类型之一保存在同一变量中。 Anyway, the point is that there are various ways to do operate on generic data in C. 无论如何,关键是要用多种方法对C中的通用数据进行操作。

What if your array consisted of C structs of the form: 如果您的数组由以下形式的C结构组成:

struct slot {
  int type;
  char *data;
};

Now you have an array that can contain arbitrary types of data, as long as you can represent them as pointers-to-char. 现在,您有了一个可以包含任意类型的数据的数组,只要您可以将它们表示为char型指针即可。

This isn't, of course, how Python does it; 当然,这不是Python的工作方式。 the point is that there doesn't have to be any connection between how your application handles data and how your implementation language handles data. 关键是您的应用程序如何处理数据与实现语言如何处理数据之间没有任何联系。

how would I create an array with different data types in it using c? 如何使用c在其中创建具有不同数据类型的数组?

You can't; 你不能 C is a statically-typed language. C是一种静态类型的语言。

You can, however, use things like unions: 但是,您可以使用诸如联合的功能:

typedef union {
    int i;
    float f;
} Foo;


Foo array[3];

array[0].i = 3;
array[1].f = 4.2;
...

You can also use void * to point at any objects you like, but this requires careful memory management. 您也可以使用void *指向任何您喜欢的对象,但这需要仔细的内存管理。

In Python, there are no “raw” values; 在Python中,没有“原始”值。 everything is an object and knows its type. 一切都是对象,并且知道其类型。 Even integers are objects (try printing (1).__class__ or 1 .__class__ ). 甚至整数都是对象(尝试打印(1).__class__1 .__class__ )。 Everything you do in C with Python objects, you do through a PyObject * (a pointer to the object).¹ 在C中使用Python对象所做的所有事情,都是通过PyObject * (指向对象的指针)完成的。¹

A Python list is a dynamic (ie resizable) array of PyObject * . Python listPyObject *的动态(即可调整大小)数组。 Since every object knows its type, the list doesn't have to be declared as having members of a specific type. 由于每个对象都知道其类型,因此不必将list声明为具有特定类型的成员。

¹ Also note: Python does not have “variables” in the usual sense (C, BASIC, Pascal etc), where a typed variable contains a value; ¹另外请注意:Python没有通常意义上的“变量”(C,BASIC,Pascal等),其中类型化变量包含一个值; it has namespaces and names (actually, dictionaries mapping strings to objects; a namespace is a dictionary, its keys are the names, its values are the pointers to the objects pointed to by each name). 它具有名称空间和名称(实际上,字典将字符串映射到对象;名称空间是字典,其键是名称,其值是指向每个名称指向的对象的指针)。

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

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