Zanurkuj w Pythonie/Formatowanie napisów w oparciu o słowniki: Różnice pomiędzy wersjami
Usunięta treść Dodana treść
m Nowa strona: <!-- Jeśli chcesz tłumaczyć, usuń poniższy komentarz i zostaw samo {{WEdycji}} --> <!-- {{WEdycji}} --> {{TODO| * Wymaga przetłumaczenia (kodów źródłowych nie tłumaczyć) * ... |
mNie podano opisu zmian |
||
Linia 11:
}}
Why did you learn about locals and globals? So you can learn about dictionary-based string formatting. As you recall, regular string formatting provides an easy way to insert values into strings. Values are listed in a tuple and inserted in order into the string in place of each formatting marker. While this is efficient, it is not always the easiest code to read, especially when multiple values are being inserted. You can't simply scan through the string in one pass and understand what the result will be; you're constantly switching between reading the string and reading the tuple of values.
There is an alternative form of string formatting that uses dictionaries instead of tuples of values.
Example 8.13. Introducing dictionary-based string formatting▼
▲'''Example 8.13. Introducing dictionary-based string formatting'''
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}▼
>>> "%(pwd)s" % params 1▼
'secret'▼
>>> "%(pwd)s is not a good password for %(uid)s" % params 2▼
'secret is not a good password for sa'▼
>>> "%(database)s of mind, %(database)s of body" % params 3▼
'master of mind, master of body'▼
▲ >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
1 Instead of a tuple of explicit values, this form of string formatting uses a dictionary, params. And instead of a simple %s marker in the string, the marker contains a name in parentheses. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker.▼
▲ >>> "%(pwd)s" % params #(1)
2 Dictionary-based string formatting works with any number of named keys. Each key must exist in the given dictionary, or the formatting will fail with a KeyError.▼
▲ 'secret'
3 You can even specify the same key twice; each occurrence will be replaced with the same value.▼
▲ >>> "%(pwd)s is not a good password for %(uid)s" % params #(2)
▲ 'secret is not a good password for sa'
▲ >>> "%(database)s of mind, %(database)s of body" % params #(3)
▲ 'master of mind, master of body'
▲
▲
So why would you use dictionary-based string formatting? Well, it does seem like overkill to set up a dictionary of keys and values simply to do string formatting in the next line; it's really most useful when you happen to have a dictionary of meaningful keys and values already. Like locals.
Example 8.14. Dictionary-based string formatting in BaseHTMLProcessor.py▼
▲'''Example 8.14. Dictionary-based string formatting in BaseHTMLProcessor.py'''
def handle_comment(self, text): ▼
self.pieces.append("<!--%(text)s-->" % locals()) 1▼
▲ def handle_comment(self, text):
▲ self.pieces.append("<!--%(text)s-->" % locals()) 1
Example 8.15. More dictionary-based string formatting▼
▲'''Example 8.15. More dictionary-based string formatting'''
def unknown_starttag(self, tag, attrs):▼
strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) 1▼
self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) 2▼
▲ def unknown_starttag(self, tag, attrs):
1 When this method is called, attrs is a list of key/value tuples, just like the items of a dictionary, which means you can use multi-variable assignment to iterate through it. This should be a familiar pattern by now, but there's a lot going on here, so let's break it down:▼
▲ strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) #(1)
▲ self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) #(2)
▲
## In the
## The string formatting ' %s="%s"' % (key, value) will resolve to ' href="index.html"'. This string becomes the first element of the list comprehension's return value. ▲2 Now, using dictionary-based string formatting, you insert the value of tag and strattrs into a string. So if tag is 'a', the final result would be '<a href="index.html" title="Go to home page">', and that is what gets appended to self.pieces.
Important
Using dictionary-based string formatting with locals is a convenient way of making complex string formatting expressions more readable, but it comes with a price. There is a slight performance hit in making the call to locals, since locals builds a copy of the local namespace.
|