Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : terminology 'class' VS 'type'

Just a simple question : when should I use the term 'class', and when should I use the term 'type' in Python ?

  • is 'class' only for user-defined types, and 'type' for built-in types ?
  • or now that everything is a type ... should I use always 'type' even for user-defined classes ?
  • ... ?
like image 682
sebpiq Avatar asked Nov 12 '10 07:11

sebpiq


People also ask

What is the difference between class and type in Python?

Once upon a time, Python had both types and classes. Types were built-in objects defined in C; classes were what you built when using a class statement. The two were named differently because you couldn't mix these; classes could not extend types.

What is difference between class and type?

The class defines object's internal state and the implementation of its operations. In contrast, an object's type only refers to its interface - a set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type.

Is class A type in Python?

Remember that, in Python, everything is an object. Classes are objects as well. As a result, a class must have a type.

What is type () in Python?

Python type() is a built-in function that returns the type of the objects/data elements stored in any data type or returns a new type object depending on the arguments passed to the function. The Python type() function prints what type of data structures are used to store the data elements in a program.


1 Answers

It is more or less historical: they used to be different a long time ago, which has no practical implications anymore.

Edit: I use "class" when referring to concrete implementations and "type" in a more informal way, when speaking about high level data structures, application arcitecture etc. In my thinking a type is a more general thing, I don't think of every class as a distinct type.

Also, when I use metaclasses (very rarely) I speak of types.

like image 119
knitti Avatar answered Oct 03 '22 00:10

knitti