PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/20110127-python-not-all-strings.html

https://bitbucket.org/obensonne/obensonne.bitbucket.org
HTML | 121 lines | 112 code | 3 blank | 6 comment | 0 complexity | e8591c8de670ebf2ba65900d38239aa3 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta name="description" content="Obvious and hidden reasons for the regular Python error." />
  6. <meta name="keywords" content="python, error, string" />
  7. <link rel="alternate" type="application/rss+xml" href="http://obensonne.bitbucket.org/rss.xml" title="Oben Sonne" />
  8. <title>Oben Sonne - blog</title>
  9. <link href="http://obensonne.bitbucket.org/style/reset.css" rel="stylesheet" type="text/css" />
  10. <link href="http://obensonne.bitbucket.org/style/960.css" rel="stylesheet" type="text/css" />
  11. <link href="http://obensonne.bitbucket.org/style/text.css" rel="stylesheet" type="text/css" />
  12. <link href="http://obensonne.bitbucket.org/style/os.css" rel="stylesheet" type="text/css" />
  13. <script type="text/javascript">
  14. /* <![CDATA[ */
  15. (function() {
  16. var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
  17. s.type = 'text/javascript';
  18. s.async = true;
  19. s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
  20. t.parentNode.insertBefore(s, t);
  21. })();
  22. /* ]]> */
  23. </script>
  24. </head>
  25. <body>
  26. <div id="wrap" class="container_12">
  27. <div id="header" class="prefix_2 grid_3">
  28. <h1><a class="plain" href="http://obensonne.bitbucket.org/index.html">Oben Sonne</a></h1>
  29. </div>
  30. <div id="menu" class="grid_5 suffix_2">
  31. <ul class="">
  32. <li class=""><a href="http://obensonne.bitbucket.org/index.html">home</a></li>
  33. <li class="current"><a href="http://obensonne.bitbucket.org/blog.html">blog</a></li>
  34. <li class=""><a href="http://obensonne.bitbucket.org/projects.html">projects</a></li>
  35. <li class=""><a href="http://obensonne.bitbucket.org/about.html">about</a></li>
  36. </ul>
  37. </div>
  38. <div class="clear"> </div>
  39. <div id="content" class="prefix_2 grid_8 suffix_2">
  40. <div class="wrap">
  41. <h1 class="post-title">TypeError: not all arguments converted during string formatting</h1>
  42. <p class="post-date">Posted on January 27, 2011</p>
  43. <p>If you're programming Pyhon, you've probably seen this before:</p>
  44. <pre><code>Traceback (most recent call last):
  45. File ...
  46. TypeError: not all arguments converted during string formatting
  47. </code></pre>
  48. <p>The reason probably is that you passed more strings after the <code>%</code> character
  49. than there are corresponding placeholders:</p>
  50. <pre><code>&gt;&gt;&gt; values = ["foo", "bar]
  51. &gt;&gt;&gt; print "values: s" % values
  52. Traceback (most recent call last):
  53. File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  54. TypeError: not all arguments converted during string formatting
  55. </code></pre>
  56. <p>The problem and its fix is simple -- add the missing <code>%</code>:</p>
  57. <pre><code>&gt;&gt;&gt; print "values: %s" % values
  58. values: ['foo', 'bar']
  59. </code></pre>
  60. <p>Not a big deal. However, recently I came across this:</p>
  61. <pre><code>&gt;&gt;&gt; print "values: %s" % values
  62. Traceback (most recent call last):
  63. File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  64. TypeError: not all arguments converted during string formatting
  65. </code></pre>
  66. <p>What? Yes, that may happen if <code>values</code> is a tuple, in which case you need as
  67. much format characters in your string as there are elements in the tuple:</p>
  68. <pre><code>&gt;&gt;&gt; values = ("foo", "bar)
  69. &gt;&gt;&gt; print "values: %s, %s" % values
  70. values: foo, bar
  71. </code></pre>
  72. <p>Of course this is a bad solution as it you have to know the tuple size when
  73. coding. Addtionally it does not really <em>dump</em> <code>value</code>, what </p>
  74. </div>
  75. </div>
  76. <div id="footer" class="prefix_2 grid_8 suffix_2">
  77. A site by <a href="http://obensonne.bitbucket.org/">Oben Sonne</a>
  78. &middot;
  79. Licensed as <a href="http://creativecommons.org/licenses/by-sa/3.0">CC-by-SA</a>
  80. &middot;
  81. Built with <a href="http://bitbucket.org/obensonne/poole">Poole</a>
  82. &middot;
  83. <a href="http://validator.w3.org/check?uri=referer">Validate me</a>
  84. </div>
  85. <div class="clear"></div>
  86. </div>
  87. <!-- analytics -->
  88. <script type="text/javascript">
  89. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  90. document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  91. </script>
  92. <script type="text/javascript">
  93. try {
  94. var pageTracker = _gat._getTracker("UA-10517530-5");
  95. pageTracker._trackPageview();
  96. } catch(err) {}</script>
  97. <!-- end: analytics -->
  98. <!-- plusone -->
  99. <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
  100. <!-- end: plusone -->
  101. <!-- disqus -->
  102. <script type="text/javascript">
  103. //<![CDATA[
  104. (function() {
  105. var links = document.getElementsByTagName('a');
  106. var query = '?';
  107. for(var i = 0; i < links.length; i++) {
  108. if(links[i].href.indexOf('#disqus_thread') >= 0) {
  109. query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
  110. }
  111. }
  112. document.write('<script charset="utf-8" type="text/javascript" src="http://disqus.com/forums/obensonne/get_num_replies.js' + query + '"></' + 'script>');
  113. })();
  114. //]]>
  115. </script>
  116. <!-- end: disqus -->
  117. </body>
  118. </html>