0
  1. What is a "variable"/data type?
  2. How does a compiler / interpreter handle types?

My specific concern has to do with the different types in lower and higher level languages. For instance in Python, as list is really flexible. You can just define it and put in at runtime whatever you want.

  • ints
  • floats
  • strings
  • sets
  • even other lists

Basically objects of any type (if I'm not mistaken).

So I imagine list to be a class written in C with really a lot of stuff in there, dynamic memory allocation and so on and so forth, to handle all this.

  • Is that right?
  • ..and does that mean all those e. g. Python types are C classes?
  • Then what are types in C?
  • Are they "classes"?
  • ..or somehow related to assembler code?

PS: I'm not looking for some precise illustrative hints and answers rather than for a wide exhaustive explanation. And: This questions are not related to "There are no variables in Python, but identifiers".

2 Answers 2

2

Data types are a way to convey information about an object or variable in a computer program. The information provided by data types is

  • How much storage does an object of this type occupy
  • What sub-elements, if any, does such an object have and by what names can these sub-elements be accessed
  • What operations can be performed on an object of this type

There are two ways that types can be handled by a compiler/interpreter, statically and dynamically. How each type is handled depends on the definition of the programming language and the implementation of the compiler/interpreter.

With static types, the compiler (these types are mostly used in compiled languages) knows at any point in the program exactly which operations are valid for a given variable/object and, if you invoke an operation, which instructions to generate or which exact function to invoke. The runtime environment does not need any access to type information.

With dynamic types, the compiler/interpreter may roughly know what operations are valid for a given variable/object, but it may not know all the details. For example, the compiler knows that variable X is of type A or a sub-class of A, but it may not know exactly which sub-class. In this case, some type information must be accessed by the runtime environment to invoke the correct function of the correct sub-class.


In C, all types are static. All type information is handled by the compiler and there is no type information stored in the executables.

In Python, all types are defined as classes and they are in principle dynamic and it is likely that type information gets stored along with the actual data. This type information makes it possible to determine (at a later time) what exact kind of data was stored in a list.

An example of a language that uses both static and dynamic types would be C++. There, most types are handled statically, except for classes that contain virtual members. Those are handled dynamically.

3
  • 1
    An important addition to this, I think, is how python treats variables. Wheras in C you can allocate something on the stack and have an actual chunk of memory as the "value" of your variable, python has every variable being closer to a pointer. A list<literally anything> in C, therefore, would be hugely complex and impossible to typecheck; a list<anything> in Python is simply a list of identically sized pointers and requires no special-casing at all.
    – Phoshi
    Commented Jan 27, 2014 at 9:35
  • In C point 2 of your list applies to structs, right? Anything else besides that?
    – user114515
    Commented Jan 28, 2014 at 21:10
  • In C, structs and arrays have sub-elements. For other types, point 2 will indicate that there are no sub-elements. Commented Jan 29, 2014 at 7:57
1

You're basically right. Python types are just wrappers around C types (technically, there are no classes in C). These wrappers implement very specifically defined behaviors for Python. In the case of lists, the Python FAQ shows that they are implemented in C as an array of pointers, and resized when necessary (such as inserting or deleted objects), with a bit of added optimization.

This is the kind of thing that programmers working in C have had to do for themselves for a long time. Python just takes care of all of this for you in the background so you don't have to worry about it.

As far as Python types being C classes, or What are C types?, you can consider C types to be the building blocks that Python is built on. In the same way that a hash table is built on lists, but with added functionality to provide a specific service (fast insert/lookup), Python types/classes are built on C types, with added functionality.

1
  • No classes in C? Hmm, I made a cdef class in cython. Now I wonder what that code actually does.. good link, btw
    – user114515
    Commented Jan 28, 2014 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.