What are and how do (data-) types work?

  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”.

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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật