<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>TechNotes</title>
	<link>http://technotes.jovianstorm.com</link>
	<description>Codename: Chona</description>
	<pubDate>Mon, 13 Aug 2007 17:45:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>
	<language>en</language>
			<item>
		<title>Structs, tipos y listas enlazadas</title>
		<link>http://technotes.jovianstorm.com/2007/08/13/structs-tipos-y-listas-enlazadas.html</link>
		<comments>http://technotes.jovianstorm.com/2007/08/13/structs-tipos-y-listas-enlazadas.html#comments</comments>
		<pubDate>Mon, 13 Aug 2007 16:07:04 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
		
	<dc:subject>Programación</dc:subject><dc:subject>c</dc:subject><dc:subject>programacion</dc:subject>
		<guid isPermaLink="false">http://technotes.jovianstorm.com/2007/08/13/structs-tipos-y-listas-enlazadas.html</guid>
		<description><![CDATA[Cada vez que retomo la programación en C después de haberme dado más de un mes para oxidarme, hay algo que, por más que lo utilice, siempre tengo que volver a consultar: las structs. Una struct nos permite tener un registro con varios campos en una sola variable, pero no voy a explicar mucho más [...]]]></description>
			<content:encoded><![CDATA[<p>Cada vez que retomo la programación en C después de haberme dado más de un mes para <em>oxidarme</em>, hay algo que, por más que lo utilice, siempre tengo que volver a consultar: las <em>structs</em>. Una struct nos permite tener un registro con varios campos en una sola variable, pero no voy a explicar mucho más aquí. Voy a hablar de las distintas formas en las que se pueden declarar, que es lo que siempre olvido.</p>
<p>La declaración de una struct tiene la forma <code class="inline">struct <em>nombre_opcional</em> { campos };</code>. La forma más usual, al menos en mi caso, de declarar una struct y luego definir una variable con ese tipo es esta:</p>
<div class="igBar"><span id="lc-9"><a href="#" onclick="javascript:showPlainTxt('c-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-9">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> complejo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">float</span> real, imaginario;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> complejo n; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>En ella creo una struct simple con dos campos, que son la parte real e imaginaria de un número complejo, y declaro la variable <em>n</em> como complejo. Aquí puede verse como el uso de una struct es similar al de un tipo, pero hay que recordar que es necesario que la declaración de la variable esté precedida de la palabra <code class="inline">struct</code>, como se ve en el ejemplo, pues los nombres de las structs no se incluyen de forma independiente en la tabla de símbolos del compilador, sólo asociados al modificador <em>struct</em>.</p>
<p><a id="more-12"></a><br />
Si no vamos a usar muchas variables del mismo tipo, podemos hacer la declaración de <em>n</em> en un sólo paso, y podemos incluso omitir el nombre de la struct (por eso es opcional), como se ve en este otro ejemplo:</p>
<div class="igBar"><span id="lc-10"><a href="#" onclick="javascript:showPlainTxt('c-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-10">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">float</span> real, imaginario;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> n; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>En este caso la variable <em>n</em> es también un número complejo, pero si quisiéramos otra variable del mismo tipo en otro lugar del programa, tendríamos que reescribir toda esta declaración.</p>
<p>Si queremos evitar evitar tener que pasar por el <em>tedioso trabajo</em> <img src='http://technotes.jovianstorm.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  de escribir la palabra struct cada vez que vamos a declarar una variable con el tipo struct complejo, podemos usar <code class="inline">typedef</code>. Typedef es una sentencia que permite crear nuevos nombres de tipo a partir de los tipos ya existentes. Su sintaxis es <code class="inline">typedef tipo_anterior alias</code>. Esto lo hacemos normalmente porque el tipo original es más complicado de escribir o porque un nuevo nombre añade información semántica de valor y hace el código más legible. Siguiendo con el ejemplo de los números complejos, podría crear un tipo llamado simplemente complejo de la siguiente forma:</p>
<div class="igBar"><span id="lc-11"><a href="#" onclick="javascript:showPlainTxt('c-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-11">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">float</span> real, imaginario;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> complejo;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">complejo n; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Como vemos, en este caso se omite el nombre de la struct, de modo que es anónima, pero no nos hará falta porque, como estamos definiendo un nuevo tipo, podemos referirnos a ella con el nombre de éste: <em>complejo</em>. Hay que prestar especial atención al hecho de que, en esta ocasión, a la hora de declarar la variable <em>n</em>, no es necesario usar la palabra struct, ahora complejo se encuentra en la tabla de símbolos del compilador de forma independiente. En este caso podríamos haber indicado un nombre a la struct, pero no nos habría servido para nada, incluso podría haberse llamado <em>complejo</em>, igual que el tipo, pues al ir asociada a la palabra struct no existirían conflictos de nombres. Más abajo hay un ejemplo que puede aclarar este punto.</p>
<p>Para seguir explicando las diferentes posibilidades voy a dejar de lado los números complejos y usaré para los ejemplos siguientes una de las estructuras clásicas con las que cualquier programador tiene que enfrentarse alguna vez: una lista simplemente enlazada. En este caso usaré una lista de enteros. Las explicaciones, no obstante, son aplicables a cualquier struct que incluya entre sus campos algún puntero a sí misma.</p>
<p>La forma más común de crear esta estructura en la siguiente:</p>
<div class="igBar"><span id="lc-12"><a href="#" onclick="javascript:showPlainTxt('c-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-12">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> nodo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> valor;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">struct</span> nodo *siguiente;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> nodo *lista; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ahora, si queremos crear un tipo nuevo para la struct que representa un nodo de la lista, <strong>NO</strong> podemos hacer esto:</p>
<div class="igBar"><span id="lc-13"><a href="#" onclick="javascript:showPlainTxt('c-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-13">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> valor</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; elem *siguiente;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> elem;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">elem *lista; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>El problema es que el tipo que se está definiendo no se encuentra disponible hasta que se ha terminado de definir (parece lógico, ¿no?), de modo que no podemos crear el puntero siguiente usando el nombre <em>elem</em>, porque en ese momento no se encuentra en la tabla de símbolos. Hay varias formas de resolver esto. La primera, y más usual, es darle un nombre a la struct y usarlo para declarar el puntero siguiente, en lugar de usar el nombre del tipo:</p>
<div class="igBar"><span id="lc-14"><a href="#" onclick="javascript:showPlainTxt('c-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-14">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> nodo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> valor;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">struct</span> nodo *siguiente;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> elem;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">elem *lista; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Pero si esta mezcla no es de tu agrado, hay una forma en la que se puede usar el nombre del nuevo tipo para el puntero siguiente, y es declarando la struct y el tipo primero y definiendo los campos después:</p>
<div class="igBar"><span id="lc-15"><a href="#" onclick="javascript:showPlainTxt('c-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-15">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> nodo elem;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> nodo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> valor;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; elem *siguiente;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">elem *lista; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Así, cuando llega la declaración del puntero siguiente, la sentencia que crea el tipo ha concluido y éste se encuentra ya en la tabla de símbolos. Aunque pueda parecer menos directa, es la que más me gusta y creo que la usaré a partir de ahora a menos que el caso concreto aconseje lo contrario. </p>
<p>Siguiendo este último ejemplo, podemos ver en este recorte lo que decía más arriba, que el nombre de la struct no es un identificador por sí sólo, así que podemos usarlo también para definir el tipo:</p>
<div class="igBar"><span id="lc-16"><a href="#" onclick="javascript:showPlainTxt('c-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C:</span>
<div id="c-16">
<div class="c">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> nodo nodo;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">struct</span> nodo <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #993333;">int</span> valor;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; nodo *siguiente;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> ;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">nodo *lista; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ahora lo más probable es que cuando vuelva a pasar un tiempo sin escribir en C, se me olviden muchas cosas excepto esta. En cualquier caso, espero que este post te haya sido de tanta utilidad como a mi <img src='http://technotes.jovianstorm.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Algunas de mis lecturas para formarme, además del libro <em>The C Programming Language</em>, de Kernighan y Ritchie, han sido estas <a href="http://www.netalive.org/codersguild/posts/1753.shtml">fuente 1</a>, <a href="http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/lists.html">fuente 2</a>, <a href="http://stsdas.stsci.edu/bps/linked_list.html">fuente 3</a> y <a href="http://www.thescripts.com/forum/post1972774-16.html">fuente 4</a>.
</p>
]]></content:encoded>
			<wfw:commentRss>http://technotes.jovianstorm.com/2007/08/13/structs-tipos-y-listas-enlazadas.html/feed/</wfw:commentRss>
		</item>
		<item>
		<title>La codificación de caracteres en Python</title>
		<link>http://technotes.jovianstorm.com/2007/02/01/la-codificacion-de-caracteres-en-python.html</link>
		<comments>http://technotes.jovianstorm.com/2007/02/01/la-codificacion-de-caracteres-en-python.html#comments</comments>
		<pubDate>Thu, 01 Feb 2007 00:26:39 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
		
	<dc:subject>Programación</dc:subject><dc:subject>python programación tips</dc:subject>
		<guid isPermaLink="false">http://technotes.jovianstorm.com/2007/02/01/la-codificacion-de-caracteres-en-python.html</guid>
		<description><![CDATA[Cuando escribimos un script en Python y éste contiene cadenas de caracteres que no están escritas en inglés, es posible que éstas contengan caracteres que no se encuentran entre los definidos por el ASCII. En este caso, si no se le dice a Python cómo debe tratarlas, éste se queja.
Tomemos, por ejemplo, este script que [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando escribimos un script en Python y éste contiene cadenas de caracteres que no están escritas en inglés, es posible que éstas contengan caracteres que no se encuentran entre los definidos por el ASCII. En este caso, si no se le dice a Python cómo debe tratarlas, éste se queja.</p>
<p>Tomemos, por ejemplo, este script que tan sólo muestra en pantalla una cadena de texto:</p>
<div class="igBar"><span id="lpython-20"><a href="#" onclick="javascript:showPlainTxt('python-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-20">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># Las tildes no están a salvo en los comentarios</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Las tildes no están a salvo en las cadenas de texto"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Si ejecutamos este script tal como está ahora, tendremos una desagradable sorpresa:</p>
<p><code>jake@aurora /tmp $ chmod u+x encoding.py<br />
jake@aurora /tmp $ ./encoding.py<br />
sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file ./encoding.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details<br />
Las tildes no están a salvo en las cadenas de texto</code></p>
<p>La URL que devuelve el intérprete corresponde a una propuesta de extensión de Python (PEP), aceptada hace tiempo, que indica <a href="http://www.python.org/dev/peps/pep-0263/" title="Defining Python Source Code Encodings">cómo especificar la codificación de caracteres</a> no ASCII.</p>
<p>Básicamente, lo que dice es que Python interpreta todo como ASCII a menos que se le indique lo contrario con <strong>un comentario especial que tiene que estar en la primera o la segunda línea</strong> y debe casar con la expresión regular: <code class="inline">coding[:=]\s*([-\w.]+)</code>. Esto significa que si usas <a href="http://www.vim.org/">Vim</a> o <a href="http://www.gnu.org/software/emacs/" title="Página del editor Emacs en gnu.org" title="Página del editor Vim">emacs</a> para editar el código python puedes utilizar la sintaxis propia de cada uno para indicarle al editor la codificación del archivo y así <em>matar dos pájaros de un tiro</em>. Además, como bien dice el artículo, se puede usar incluso el lenguaje natural; podría escribir:</p>
<div class="igBar"><span id="lpython-21"><a href="#" onclick="javascript:showPlainTxt('python-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-21">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># Este archivo usa el encoding: utf-8</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># Las tildes no están a salvo en los comentarios</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Las tildes no están a salvo en las cadenas de texto"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Aunque yo prefiero usar la sintaxis de Vim, que es, dicho sea de paso, mi editor favorito <img src='http://technotes.jovianstorm.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="igBar"><span id="lpython-22"><a href="#" onclick="javascript:showPlainTxt('python-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-22">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># vim: set fileencoding=utf-8 :</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"># Las tildes no están a salvo en los comentarios</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">"Las tildes no están a salvo en las cadenas de texto"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Y si ejecutamos esta nueva versión vemos que ahora no hay quejas:</p>
<p><code>jake@aurora /tmp $ ./encoding.py<br />
Las tildes no están a salvo en las cadenas de texto</code></p>
]]></content:encoded>
			<wfw:commentRss>http://technotes.jovianstorm.com/2007/02/01/la-codificacion-de-caracteres-en-python.html/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
