user1559873
user1559873

Reputation: 6876

Are Python data types classes, or data structures?

I am new to Python. I referred to different tutorials on it and found some contradictions.

Some authors say that in Python, numbers (int, float, complex), lists, sets, tuples, dictionaries, strings are data types.
Some authors call them data structures, few authors say classes. I am confused about which one is correct.

I'm doing an essay on Python, and wondering if anyone could clarify and justify the answer.

Upvotes: 3

Views: 8565

Answers (3)

andrew cooke
andrew cooke

Reputation: 46872

exact meanings have changed slightly over time. the latest version of python (python 3) is the simplest and most consistent, so i will explain with that.


let's start with the idea that there are two kinds of things: values and the types of those values.

a value in python can be, for example, a number, a list, or even a function.

types of values describe those. so the type of a number might be int for example.

so far we have only considered things "built in" to the language. but you can also define your own things. to do that you define a new class. the type() function will say (in python 3) that the type of an instance of your class is the class itself:

so maybe you define a class called MyFoo:

>>> class MyFoo:
>>>     def __init__(self, a):
>>>         self.a = a
>>>
>>> foo = MyFoo(1)
>>> type(foo)
<class '__main__.MyFoo'>

compare that with integers:

>>> type(1)
<class 'int'>

and it's clear that the type of your value is its class.

so values in python (eg numbers, lists, and even functions) are all instances of classes. and the type of a value is the class that describes how it behaves.

now things get more complicated because you can also assign a type to a value! then you have a value that is a type:

>>> x = type(1)
>>> type(x)
<class 'type'>

it turns out that type of any type is type. which means that any class is itself an instance (of type). which is all a little weird. but it's consistent and not something you need to worry about normally.


so, in summary, for python 3 (which is simplest):

  • every value has a type
  • the type describes how the value works
  • types are classes
  • the type of an instance of a class is its class
  • numbers, lists, functions, user-defined objects are all instances of classes
  • even classes are instances of classes! they are instances of type!

finally, to try answer your exact question. some people call classes data types and the instances data structures (i think). it's messy and confusing and people are not very careful. it's easiest to just stick with classes and types (which are the same thing really) and instances.

Upvotes: 3

Andrew Clark
Andrew Clark

Reputation: 208635

The terms "data type" and "class" are synonymous in Python, and they are both correct for the examples you gave. Unlike some other languages, there are no simple types, everything (that you can point to with a variable) in Python is an object. The term "data structure" on the other hand should probably be reserved for container types, for example sets, tuples, dictionaries or lists.

Upvotes: 3

abarnert
abarnert

Reputation: 366003

A "data type" is a description of a kind of data: what kinds of values can be an instance of that kind, and what kind of operations can be done on them.

A "class" is one way of representing a data type (although not the only way), treating the operations on the type as "methods" on the instances of the type (called "objects"). This is a general term across all class-based languages. But Python also has a specific meanings for "class": Something defined by the class statement, or something defined in built-in/extension code that meets certain requirements, is a class.

So, arbitrary-sized integers and mapping dictionaries are data types. in Python, they're represented by the built-in classes int and dict.

A "data structure" is a way of organizing data for efficient or easy access. This isn't directly relevant to data types. In many languages (like C++ or Java), defining a new class requires you to tell the compiler how an instance's members are laid out in memory, and things like that, but in Python you just construct objects and add members to them and the interpreter figures out how to organize them. (There are exceptions that come up when you're building extension modules or using ctypes to build wrapper classes, but don't worry about that.)

Things get blurry when you get to higher-level abstract data structures (like pointer-based nodes) and lower-level abstract data types (like order-preserving collection of elements that can do constant-time insertion and deletion at the head). Is a linked list a data type that inherently requires a certain data structure, or a data structure that defines an obvious data type, or what? Well, unless you major in computer science in college, the answer to that isn't really going to make much difference, as long as you understand the question.

So, mapping dictionaries are data types, but they're also abstract data structures—and, under the covers, Python's dict objects are built from a specific concrete data structure (open-chained hash table with quadratic probing) which is still partly abstract (each bucket contains a duck-typed value).

Upvotes: 3

Related Questions