[Tutor] Difference between types

Steven D'Aprano steve at pearwood.info
Fri May 24 06:45:39 CEST 2013


On 24/05/13 05:57, Citizen Kant wrote:
> I guess I'm understanding that, in Python, if something belongs to a type,
> must also be a value.

Everything in Python is a value, and everything belongs to a type. Including other types. Yes, types are values too, and types have types of their own. There is no infinite regress, because ultimately everything ends at the "magic" type called "type", which is its own type:


py> type(42)  # 42 is an instance of int
<class 'int'>
py> type(int)  # int is an instance of type
<class 'type'>
py> type(type)  # type is an instance of itself!
<class 'type'>


> I guess I'm understanding that the reason why 9 is considered a value, is
> since it's a normal form*,* an element of the system that cannot be
> rewritten and reduced any further.

Are you referring to this definition?

http://en.wikipedia.org/wiki/Value_%28computer_science%29

As far as it goes, that's not an unreasonable rule of thumb, but it isn't bullet-proof. What, for example, do you make of this:

0x09

Is that a value? I say yes, because that is a literal expression for the natural number 9. It merely happens to use hexadecimal notation instead of decimal notation. Another language might define keywords one, two, three ... nine, ten which are also literal expressions for the same natural number, only using English words instead of numerals.

But let's not worry about hypothetical languages with such keywords. Even in Python, each of these represent the exact same value:

9 0x09 0o10 0b1001

They are the same value, using different notation.


> I also guess I'm understanding that the same goes somehow for the letter A
> for example, since it cannot be rewritten or reduced any further, so it's a
> value too.

Likewise for every object. Compound objects are also values:

iter([12, 3, None, "hello world!", 4.5, ('a', 'b', 3), {}])


but the fact that it is a value is not really captured by the definition given above. The definition is true so far as it goes, but it doesn't go far enough.


> type('A')
> <type 'str'>
>
> The question is, in order to understand: does this apostrophes thing has a
> more profound reason to be attached to the letters or it's just a
> conventional way to set a difference between letters and numbers?

The later. It is a long standing tradition in computer programming that so-called "strings" (ordered sequences of letters, digits and other characters) are defined using a notation that includes quotation marks as delimiters. In contrast, numbers are written in straight numeric form, usually in base-10 but sometimes in other bases.

This is not a hard rule, but it is a strong tradition, and any language which did differently would be considered strange. Some languages give '' and "" different meanings, but Python does not.


>  Do I must
> observe this apostrophes thing like the symbol of the type itself inside
> which one can put any character, setting it as type str?

I'm afraid that I don't understand the question.


-- 
Steven


More information about the Tutor mailing list