Over 30 types of variables available in python ?
steve+comp.lang.python at pearwood.info
Mon Jan 7 10:52:02 CET 2013
On Mon, 07 Jan 2013 00:53:26 -0800, chaouche yacine wrote:
> Thanks for all your comments. It appears to me that there is a slight
> confusion between types and classes then, plus other entities (protocols
In Python 3, types and classes are synonyms. They mean the same thing.
In Python 2, there is a very subtle difference, due to the existence of
"old style" or "classic" classes, for backward compatibility, which are
not identical to "new style" classes, also known as types. But they are a
"kind of type" -- although there are some differences, you can consider
them to be the same sort of thing.
> So my question is : is there a notion of "type" in python, like in other
> languages (integers, booleans, floats, strings, characters (in c)) ? if
> so, can you give a list of the available "types" ?
Yes, and no.
Yes, there is a notation of type in Python which is exactly the same sort
of notation of type in other languages, such as C. The difference between
C and Python is not in the notation of "type", but in the notation of
"variable" or "name".
In C, *variables* have a type. If you declare a variable x to be a float,
then three things happen:
* the C compiler creates a fixed memory location and calls it "x";
* the C compiler interprets the *value* of that memory location (which is
actually just a bunch of bytes) as a float;
* the C compiler will only ever place values into that memory location if
it thinks that the value is a float, or compatible with a float.
Because all values are bunches of bytes, you can (with a bit of effort)
grab the bytes from any location, without caring what the type the
compiler considers it. Sometimes this is useful; more often it is a
source of bugs.
In Python, *names* have no type, but *objects* (values) do. Because names
are untyped, the one name (say, x) might be store a float one minute,
then later have a list assigned to it, then a string, then a float again.
But at any instant, whatever the value of x, it is an object, and objects
always have a type no matter what name they are bound to. The type
information is *part of the object*, rather than part of the name.
Because Python considers all objects to be typed, there is no concept of
extracting the raw bytes from a list. (Of course lists actually are made
out of bytes, but you cannot access them from pure Python code.)
So, yes, Python has types, just as C has types, but the difference is
that Python associates the type with the *value* (object) rather than a
storage location (variable).
But no, we can't give a complete list of all types, because there is no
limit to the number of types. There are a certain number of built-in
types, plus many more types which are available from the standard
library, plus an infinite number of custom types you can create yourself.
You can get a list of the common built-in types and the standard library
types from the Fine Manual:
There are also built-in types which are essentially undocumented, and
used as implementation details of higher-level objects like functions,
methods, and so forth. You are not expected to use these directly.
Instead, you just use the function itself.
If you are unfamiliar with Object Oriented Programming, you can broadly
speaking consider types to be like smart structs that carry code around
with them. There is no limit to the number of possible structs, and
likewise there is no limit to the number of possible types.
> The documentation (http://docs.python.org/2/library/stdtypes.html)
> states "The principal built-in types are numerics, sequences, mappings,
> files, classes, instances and exceptions."
Yes, they are the principal types, but there are many others.
There are three different built-in numeric types:
plus Decimal and Fraction in the standard library;
There are two standard built-in sequence types:
plus others in the standard library, such as namedtuple;
There is one standard built-in mapping type:
plus at least two more in the standard library, defaultdict and
etc. You can read the docs more easily than I can copy the types out.
More information about the Python-list