<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 03/06/2014 11:46 PM, Cr&iacute;stian Viana
      wrote:<br>
    </div>
    <blockquote cite="mid:5318985B.7000600@linux.vnet.ibm.com"
      type="cite">
      <meta content="text/html; charset=ISO-8859-1"
        http-equiv="Content-Type">
      Am 06-03-2014 12:12, schrieb Sheldon:<br>
      <blockquote cite="mid:5318907A.1070406@linux.vnet.ibm.com"
        type="cite">
        <meta content="text/html; charset=ISO-8859-1"
          http-equiv="Content-Type">
        AFAK&#65292; seems two types has no string representation, one is
        unicode and another is object derived from nothing.<br>
      </blockquote>
      The statement <tt>if not isinstance(value, unicode)</tt>, which
      is in the sample code above, makes sure that we will not try to
      convert a unicode object to string (only those who have a
      different type).<br>
      <br>
      And an object derived from nothing also has a string
      representation. Take a look at this example:<br>
      <br>
      <tt>&gt;&gt;&gt; class X():</tt><tt><br>
      </tt><tt>...&nbsp;&nbsp;&nbsp;&nbsp; def x(self):</tt><tt><br>
      </tt><tt>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass</tt><tt><br>
      </tt><tt>... </tt><tt><br>
      </tt><tt>&gt;&gt;&gt; a = X()</tt><tt><br>
      </tt><tt>&gt;&gt;&gt; print str(a)</tt><tt><br>
      </tt><tt>&lt;__main__.X instance at 0x7f8fe13d4b00&gt;</tt><tt><br>
      </tt><tt>&gt;&gt;&gt; print "this is a string: %s." % a</tt><tt><br>
      </tt><tt>this is a string: &lt;__main__.X instance at
        0x7f8fe13d4b00&gt;.</tt><br>
    </blockquote>
    <br>
    You forgot one. The a follow string format will also do string
    representation.<br>
    <br>
    In [2]: class a(): pass<br>
    In [9]: def toU1(value):<br>
    &nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp; if not isinstance(value, unicode):<br>
    &nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value = unicode(str(value), 'utf-8')<br>
    &nbsp;&nbsp; ...:&nbsp;&nbsp;&nbsp;&nbsp; return u"f&#466;&#466; = %s" % value<br>
    &nbsp;&nbsp; ...: <br>
    <br>
    In [10]: def toU2(value):<br>
    &nbsp;&nbsp; ....:&nbsp;&nbsp;&nbsp;&nbsp; if isinstance(value, str):<br>
    &nbsp;&nbsp; ....:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value = unicode(str(value), 'utf-8')<br>
    &nbsp;&nbsp; ....:&nbsp;&nbsp;&nbsp;&nbsp; return u"f&#466;&#466; = %s" % value<br>
    &nbsp;&nbsp; ....: <br>
    <br>
    In [11]: print toU1(a)<br>
    f&#466;&#466; = __main__.a<br>
    <br>
    In [12]: print toU2(a)<br>
    f&#466;&#466; = __main__.a<br>
    <br>
    In [13]: print toU1("f&#466;&#466;")<br>
    f&#466;&#466; = f&#466;&#466;<br>
    <br>
    In [14]: print toU2("f&#466;&#466;")<br>
    f&#466;&#466; = f&#466;&#466;<br>
    <br>
    In [15]: print toU1(u"f&#466;&#466;")<br>
    f&#466;&#466; = f&#466;&#466;<br>
    <br>
    In [16]: print toU2(u"f&#466;&#466;")<br>
    f&#466;&#466; = f&#466;&#466;<br>
    <br>
    <br>
    if one developer define a class like this, it should tell the ptyhon
    this a unicode file. <br>
    In [17]: class af&#466;&#466;(): pass<br>
    We have discuss this problem before, kimchi do not like this. <br>
    <br>
    In this case, ptyhon take a string are unicode, it should OK.<br>
    Not try.<br>
    &nbsp;<br>
    <br>
    <blockquote cite="mid:5318985B.7000600@linux.vnet.ibm.com"
      type="cite"> <br>
      <blockquote cite="mid:5318907A.1070406@linux.vnet.ibm.com"
        type="cite">But I'm not worry about it.<br>
        IMO, no one will not pass this instance of object to&nbsp;
        KimchiException as args.<br>
      </blockquote>
      IMO, we should never trust that the users/developers will pass the
      correct parameters to our code. Eventually, someone will forget
      that, and then we will have one more bug ;)<br>
    </blockquote>
    your example make me do not need to worry about this.&nbsp; <br>
    <br>
    <tt>&gt;&gt;&gt; class X():</tt><tt><br>
    </tt><tt>...&nbsp;&nbsp;&nbsp;&nbsp; def x(self):</tt><tt><br>
    </tt><tt>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass</tt><tt><br>
    </tt><tt>... </tt><tt><br>
    </tt><tt>&gt;&gt;&gt; a = X()</tt><tt><br>
    </tt><tt>&gt;&gt;&gt; print str(a)</tt><tt><br>
    </tt><tt>&lt;__main__.X instance at 0x7f8fe13d4b00&gt;</tt><tt><br>
    </tt><tt>&gt;&gt;&gt; print "this is a string: %s." % a</tt><tt><br>
    </tt><tt>this is a string: &lt;__main__.X instance at
      0x7f8fe13d4b00&gt;.</tt><br>
    <br>
    In [20]: print a<br>
    &lt;__main__.X instance at 0x206c440&gt;<br>
    <br>
    In [21]: print u"%s" % a<br>
    &lt;__main__.X instance at 0x206c440&gt;<br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Thanks and best regards!

Sheldon Feng(&#20911;&#23569;&#21512;)<a class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com">&lt;shaohef@linux.vnet.ibm.com&gt;</a>
IBM Linux Technology Center</pre>
  </body>
</html>