Some python syntax that I'm not getting

Discussion in 'Python' started by waltbrad, Dec 7, 2007.

  1. waltbrad

    waltbrad Guest

    Hello. Been studying Python for about a week now. I did a quick read
    of the tutorial in the manual and I'm reading Programming Python by
    Mark Lutz. I'm still getting used to the Python syntax, but I'm able
    to pretty much follow what is being said. But tonight Lutz was
    talking about implementing a database on a website and threw out this
    piece in his code:

    <tr><th>key<td><input type=text name=key value="%(key)s">

    That last bit is the bit that throws me: %(keys)s

    He explains this so:

    "The only feat of semimagic it relies on is using a record's attribute
    dictionary (__dict__) as the source of values when applying string
    formatting to the HTML reply template string in the last line of the
    script. Recall that a %(key)code replacement target fetches a value by
    key from a dictionary:
    '5 => shrubbery' "


    I understand how D['say'] gets you 5, But I still don't understand
    the line after the 5.

    How is the character 's' some special code? And I don't get what is
    going on with the % character. I'm used to it's use in c-style
    formatting, but this just seems so bizarre. I can tell that the key
    is being replaced by it's value in the string, but I don't know how
    that is being done.

    waltbrad, Dec 7, 2007
    1. Advertisements

  2. waltbrad

    Chris Guest

    The '%' invokes the formatter, the 's' specifies string type.
    Chris, Dec 7, 2007
    1. Advertisements

  3. Chris a écrit :
    And the (name) specify to find the value to format (following %s rules)
    in a dictionnary given as % operator parameter, under the name key.
    Laurent Pointal, Dec 7, 2007
  4. waltbrad

    Chris Mellon Guest

    Python string interpolation is based on the C printf style:
    "one two %s" % ("three",)

    is basically the same as
    printf("one two %s", "three")

    (% being overload for strings to do formatting).

    Used this way, as in C, each wildcard in the format string is replaced
    in order with the corresponding item in the format arguments, which is
    a sequence.

    However, you can also provide a dictionary (that is, a key/value
    mapping) to the string formatter, and use the %(key)s format. The name
    in the parenthesis is looked up in the provided dictionary, rather
    than by position.

    This is basically the same thing as the difference between positional
    and named arguments.
    Chris Mellon, Dec 7, 2007
  5. waltbrad

    Matt Guest

    Think of it as using a name instead of a position for your "%s".

    In addition to what others already said, I thought I'd add an example
    of where this is useful.
    One place where you don't just want to have a position is when doing
    When translating for example:
    "I'm going by %(transport)s to %(place)s" % ( {'transport' : 'car',
    'place' : 'mexico'} )

    In another language, the 'place' and 'transport' might not be in the
    same order in the sentence, so pretty much the only way to get it
    right is to use named parameters instead of positions.
    Matt, Dec 8, 2007
  6. Of course, not mentioned is the oldie "mail-merge" process...


    TEMPLATE = """
    Spam, Inc.
    221b Parrot Lane

    %(title)s %(given)s %(last)s
    %(city)s, %(state)s %(postcode)s

    Dear %(title)s %(last)s:

    Spam, Inc. is happy to announce the availability
    of our direct home delivery service in %(city)s. Now,
    you too, %(given)s, can receive our full product line,
    delivered right to your door at %(street)s....

    VICTIMS = [ {"title" : "Miss",
    "given" : "Little Red",
    "last" : "Riding-Hood",
    "street" : "Granny's House",
    "city" : "Forest",
    "state" : "Disbelief",
    "postcode" : "3.14159" } ]

    for v in VICTIMS:
    print TEMPLATE % v


    Spam, Inc.
    221b Parrot Lane

    Miss Little Red Riding-Hood
    Granny's House
    Forest, Disbelief 3.14159

    Dear Miss Riding-Hood:

    Spam, Inc. is happy to announce the availability
    of our direct home delivery service in Forest. Now,
    you too, Little Red, can receive our full product line,
    delivered right to your door at Granny's House....


    Using the dictionary form allows values to be used multiple times
    without needing to duplicate them on a positional parameter list.
    Wulfraed Dennis Lee Bieber KD6MOG

    (Bestiaria Support Staff: )
    Dennis Lee Bieber, Dec 8, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.