Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content
Stack Overflow
  1. About
  2. For Teams
Loading…
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Timeline forString formatting: % vs. .format vs. f-string literal

Current License: CC BY-SA 3.0

14 events
when toggle formatwhatbylicensecomment
Jan 14, 2015 at 19:28historyeditedthe Tin ManCC BY-SA 3.0
edited body
Oct 21, 2014 at 19:49commentadded David Sanders You can defer formatting with any kind of formatting style. You're not restricted to python modulo formatting. The logging module converts objects to strings before emitting log messages. Therefore, any object that implements__str__ will do. That could include a class which accepts a new-style formatting string with its parameters but doesn't perform the operation until the logging module calls__str__. See my answer below.
Feb 14, 2014 at 11:03commentadded trojjer If you're concerned about performance, use literal dict {} syntax instead of a dict() class instantiation:doughellmann.com/2012/11/…
Oct 10, 2013 at 12:26commentadded Jonathan Hartley Oh! I see. I took your meaning backwards. Beg your pardon. Yep, I could imagine an aggregator that tried to do that, but it seems silly. Hugs!
Oct 10, 2013 at 12:24commentadded Wooble No, I meant that a sufficiently sophisticated system could just find the similarities and group them together even if you use string formatting. Not that I'm aware of whether any can actually do this.
Oct 10, 2013 at 12:22commentadded Jonathan Hartley Hey @Wooble. I don't see how. Doing this is absolutely trivial (i.e. can be implemented in the aggregator in about three characters, literally), and if it doesn't do this, then in what sense is it a useful aggregator?
Oct 10, 2013 at 10:12commentadded Wooble @JonathanHartley: depends on how sophisticated your aggregator is.
Oct 10, 2013 at 8:04commentadded Jonathan Hartley The primary benefit of this is not performance (doing the string interpolation will be quick compared to whatever you're doing with the output from logging, e.g displaying in a terminal, saving to disk) It is that if you have a logging aggregator, it can tell you "you got 12 instances of this error message", even if they all had different 'some_info' values. If the string formatting is done before passing the string to log.debug, then this is impossible. The aggregator can only say "you had 12 different log messages"
Mar 12, 2013 at 10:17commentadded smci @snth I just split the OP's question into two separate questions. But this good answer unifies both.
Jan 30, 2013 at 21:55commentadded Cito @VinaySajip: Cool, thanks for the info and sorry for spreading misinformation. It's also in the "What's new in 3.2" article here:docs.python.org/3.2/whatsnew/3.2.html#logging. Very useful.
Jan 30, 2013 at 19:56commentadded Vinay Sajip @Cito: See this:plumberjack.blogspot.co.uk/2010/10/…
Nov 25, 2012 at 17:00commentadded Cito You can use dict formatting like this:log.debug("some debug info: %(this)s and %(that)s", dict(this='Tom', that='Jerry')) However, you can't use the new style.format() syntax here, not even in Python 3.3, which is a shame.
Nov 14, 2012 at 7:36commentadded snth This is some useful info that I just learned now. It's a pity it doesn't have it's own question as it seems separate to the main question. Pity the OP didn't split his question in two separate questions.
Feb 22, 2011 at 19:21history answeredWoobleCC BY-SA 2.5

[8]ページ先頭

©2009-2025 Movatter.jp