[Tutor] Fwd: Difference between types

Steven D'Aprano steve at pearwood.info
Sun May 26 10:14:42 CEST 2013


On 25/05/13 02:52, Citizen Kant wrote:
> When I say "coding", anyone can think about what coding is
> in his own daily work, but that's not my way.
> I'll try to refine the concept: right now I'm learning,
> if I say "coding" I refer to what I type inside
> my file named learningpythoncode.py that,
> believe it or not, starts with:
>
> # """After the hash can go my comment.
> That's the first thing I've learned, and it's useful since
> this way I don't feel like as don't knowing any language
> at all. This is part of the code that Python doesn't
> compute, but this isn't less code.
>
> Since I am using the triple quotation, I can continue:

That actually doesn't work. Because you start the first line with a hash mark # Python treats that line as a comment, including the triple-quote marks. That means that the next line will give you a SyntaxError:


py> # """This is not a triple-string
... even though it starts with a """
   File "<stdin>", line 2
     even though it starts with a """
               ^
SyntaxError: invalid syntax


When you use # for comments, each line must begin with a # mark.

Alternatively, you can use strings as pseudo-comments, including triple-quoted (multi-line) strings. Using strings is special. If the first line of code in a module, or a class, or a function, is a string, it gets turned into a "docstring" (documentation string). Python copies it into a special __doc__ attribute of the module (or class, or function) where it can be accessed as documentation.

Otherwise, a bare string has no effect in your code, since it's just data, never touched, never used, it doesn't do anything. That makes it a def facto comment. In fact, the standard, reference implementation of Python, (so-called) CPython, treats such bare strings as comments and drops them from the code when compiling it. (But even if it didn't, it would still be an effective comment, since it has no effect.)

[...]
> Then I consider to code another value, like A, that looks like if
> cannot be reduced by rewriting any further but I arrived
> to the fact that Python doesn't consider those characters like A
> as able to take part in its computations until I put them between
> apostrophes or quotation marks."""

Not at all. If you actually try it, you will see that Python *can* use a character like A as part of its computations. It is a *name*, and when you refer to it, Python tries to look up the value of A and fails, so you get an error:

py> A
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined


Python even tells you *exactly* what the problem is, if you read the error message:

- it is an error to do with names, NameError;

- specifically the name A is not defined, so there is no value to retrieve.


So we can fix this:

py> A = 9
py> A
9


This is where the English language is ambiguous. If I talk about A, am I talking about the *name* A or the *value* of A (in this case, 9)? The convention *when writing English* is to say that referring to A alone means we care about the value, and if we want to refer to the name specifically, we put it in quotation marks, 'A'.

This is unfortunately still ambiguous, since it isn't clear whether 'A' refers to the name or the letter of the alphabet, or perhaps quoting somebody. But in context, it is usually clear.

By analogy: consider the difference between the current Queen of England, and the hereditary position of Queen of England. The "value" (a person) of "the Queen of England" is currently Her Majesty Elizabeth II, an actual person, but the position itself is more abstract, and has been held by many different people, such as Queen Elizabeth I and Queen Victoria, and sometimes not held at all.

(When the ruling monarch is a man, his wife, if any, is the Queen Consort, not the Queen. And the widow of a deceased King is the Dowager Queen, or in the case of the late mother to Queen Elizabeth, Queen Elizabeth the Queen Mother. And of course the rules are different in other countries. Aren't titles fun?)

If Great Britain becomes a republic and abolishes the monarchy, she would presumably either take the surname of her husband, and become Elizabeth Mountbatten, or she would take on the name of the Royal House, and become Elizabeth Windsor.

To put this in Python terms, we might decide that the name "queen" was to refer to the current Queen of England. So:


# Elizabeth ascends to the throne.
queen = "Elizabeth Alexandra Mary"

# Should she abdicate or die, there is no longer a
# ruling queen, but the position remains.
queen = None

# If Great Britain abolishes the monarchy,
# even the position goes away.
liz_mountbatten = queen
del queen


So we can talk about the *name* (variable) "queen", or the *value* queen.



> 'A' # """ I put A between apostrophes and it suddenly turned to something
> that cannot be reduced by rewriting any further,
> or unless that seems to me.
> I thought: if one can turn to value anything by putting it between
> apostrophes or quotation marks, then the trick must be in the
> pair of apostrophes or quotation marks.

No, it is not that you are turning it into a value by putting it between quotation marks, but that you are turning it into a literal string. Strings are just one type of many, many different sorts of values:

None  # a value representing nothingness
42  # a value representing the whole number between forty-one and forty-three
[]  # a value representing an empty list
[42, 23]  # a value representing a list with two values



[...]
> Then any other Python keyword, (def for example) comes to be a
> built-in value since it's (so to speak) built-in
> assigned to one of the above values, or assigned to a name
> that was previously assigned to one of the above values.

No, def is not a value. def is a keyword, that is, a mere part of the language itself, not a value which can be operated on within the language. You can't have a def on its own, that gives a SyntaxError:

py> def
   File "<stdin>", line 1
     def
       ^
SyntaxError: invalid syntax


Nor can you bind the alleged value def to a name:

py> a = def
   File "<stdin>", line 1
     a = def
           ^
SyntaxError: invalid syntax


Of course you can create a string "def", consisting of the three letters d e and f, by putting quotation marks around it, but that's a completely different thing.




-- 
Steven


More information about the Tutor mailing list