one-element tuples [Was: Most probably a stupid question, but I still want to ask]

Ben Finney ben+python at benfinney.id.au
Sun Apr 10 20:30:22 EDT 2016


Fillmore <fillmore_remove at hotmail.com> writes:

> Sorry guys. It was not my intention to piss off anyone...just trying
> to understand how the languare works

Frustration is understandable when learning something new :-) Hopefully
that can be a signal to take a breath, and compose messages to minimise
frustration for the readers.

> I guess that the answer to my question is: there is no such thing as a
> one-element tuple,

Certainly there is. Tuples – like any container type – can contain zero,
one, or more items.

    >>> foo = ()
    >>> bar = ("spam",)
    >>> baz = ("spam", "eggs")
    >>> blub = ("spam", "eggs", "beans")
    >>> len(foo)
    0
    >>> len(bar)
    1
    >>> len(baz)
    2
    >>> len(blub)
    3

> and Python will automatically convert a one-element tuple to a
> string...

You appear to be confusing text representation, and program syntax, and
the values themselves.

Program syntax is the structured text you type, in order that Python
should compile it and result in program code. That source code is text
with a specific representation of values and statements.

The values themselves are what exist within the Python process while it
is executing your compiled code. There is no text representation of
that, Python manipulates the values in an internal representation
normally inaccessible to the program itself.

The text representation that objects can generate on request is yet
another matter; for convenience it sometimes matches a syntactic text
that could be used to construct an equal value, but often that's not the
case. It should not be thought of as any kind of universal rule.

> Did I get this right this time?

I hope that helps to distinguish the different matters that are
confusing you.

-- 
 \        “Last year I went fishing with Salvador Dali. He was using a |
  `\          dotted line. He caught every other fish.” —Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list