Two standard libraries?

Kirk McDonald kirklin.mcdonald at gmail.com
Tue Jul 31 20:51:28 PDT 2007


Chris Nicholson-Sauls wrote:
> Here's an odd thought: "%{08x:X}"
> Wherein the {}'s mean, "this is looking at a variable outside", and the 
> ':' means "everything after this point is the variable's name".
> 
> -- Chris Nicholson-Sauls

I always rather liked Python's syntax for this.

http://docs.python.org/lib/typesseq-strings.html

 >>> '%(X)08x' % {'X' : 12}
'0000000c'

That is, the identifier is placed inside of parentheses between the '%' 
and the rest of the format string. The primary difference, here, is that 
Python then expects a dictionary instead of positional format arguments. 
You can get a dictionary of the current scope's variables with the 
locals() function, thus you could easily say:

 >>> a = 'hello'
 >>> b = 'world'
 >>> '%(a)s %(b)s' % locals()
'hello world'

This part is not as applicable to D (since its AAs must be statically 
typed), but I do like the format string syntax.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list