A Deepness in the Sky (Zones of Thought, #2)
Review:Not nearly as much fun as the first two books in the series. Plus the end is left wide open, surely because the author is writing a sequel. |
Review:Not nearly as much fun as the first two books in the series. Plus the end is left wide open, surely because the author is writing a sequel. |
This has been a repeated discussion in the Python Argentina mailing list. Since it has not come up in a while, why not recap it, so the next time it happens people can just link here.
Some people for some reason do this:
>>> a = 2
>>> b = 2
>>> a == b
True
>>> a is b
True
And then, when they do this, they are surprised:
>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False
They are surprised because "2 is 2" makes more intuitive sense than "1000 is not 1000". This could be attributed to an inclination towards platonism, but really, it's because they don't know what is is.
The is operator is (on CPython) simply a memory address comparison. if objects a and b are the same exact chunk of memory, then they "are" each other. Since python pre-creates a bunch of small integers, then every 2 you create is really not a new 2, but the same 2 of last time.
This works because of two things:
Integers are read-only objects. You can have as many variables "holding" the same 2, because they can't break it.
In python, assignment is just aliasing. You are not making a copy of 2 when you do a = 2, you are just saying "a is another name for this 2 here".
This is surprising for people coming from other languages, like, say, C or C++. In those languages, a variable int a will never use the same memory space as another variable int b because a and b are names for specific bytes of memory, and you can change the contents of those bytes. On C and C++, integers are a mutable type. This 2 is not that 2, unless you do it intentionally using pointers.
In fact, the way assignment works on Python also leads to other surprises, more interesting in real life. For example, look at this session:
>>> def f(s=""):
... s+='x'
... return s
...
>>> f()
'x'
>>> f()
'x'
>>> f()
'x'
That is really not surprising. Now, let's make a very small change:
>>> def f(l=[]):
... l.append('x')
... return l
...
>>> f()
['x']
>>> f()
['x', 'x']
>>> f()
['x', 'x', 'x']
And that is, for someone who has not seen it before, surprising. It happens because lists are a mutable type. The default argument is defined when the function is parsed, and every time you call f() you are using and returning the same l. Before, you were also using always the same s but since strings are immutable, it never changed, and you were returning a new string each time.
You could check that I am telling you the truth, using is, of course. And BTW, this is not a problem just for lists. It's a problem for objects of every class you create yourself, unless you bother making it immutable somehow. So let's be careful with default arguments, ok?
But the main problem about finding the original 1000 is not 1000
thing surprising is that, in truth, it's uninteresting. Integers are fungible. You don't care if they are the same integer, you only really care that they are equal.
Testing for integer identity is like worrying, after you loan me $1, about whether I return you a different or the same $1 coin. It just doesn't matter. What you want is just a $1 coin, or a 2, or a 1000.
Also, the result of 2 is 2 is implementation dependent. There is no reason, beyond an optimization, for that to be True.
Hoping this was clear, let me give you a last snippet:
>>> a = float('NaN')
>>> a is a
True
>>> a == a
False
UPDATE: lots of fun and interesting comments about this post at reddit and a small followup here
Hello, my name is Roberto Alsina. You may know me from my appearances in "KDE Developers in the Stone Age" and "PyQt programmers gone wild".
On the other hand, I am not:
Roberto Alsina, who lives in Houston and is active in the Argentine expatriate community.
Amado Roberto Alsina, paraguayan politician.
Roberto Ariel Alsina (aka Roberstorm) who drives a motorcycle.
Roberto Alsina, puertorican architect.
Roberto Alsina, who lives in Villa Alemana and attended Colegio Buckingham.
Roberto Alsina Ruibal, who I hope is wearing a wig.
Roberto Gonzales Alsina, who lives in Canelones. I only eat canelones.
The FEARED Roberto Nava Alsina who, while only 15 years old, is scoring goals in some amateur league somewhere.
Roberto Lebron Alsina (I have slightly more hair)
Roberto Antonio Gómez Alsina, activist against bullfighting
Hopefully, this will clear things up. Thanks for reading.
About a year ago, I wrote a small web browser, called De Vicenzo just for fun.
But hey, someone went and madeit useful for something! Specifically, to provide previews when doing sphix docs
That's cool :)
This post makes no sense in english, so spanish only!
Drácula es un libro muy particular. Casi todo el mundo cree que sabe de qué se trata, pero en el 90% de los casos no es así. O sea, sí, saben que es de un vampiro, blabla.
Lo que no saben es nada del libro. Saben de las películas, del especial de Scooby Doo, delos chistes de vampiros, y cosas así, pero el libro en sí, no lo han leído.
¡Y es una lástima! Es un libro muy interesante. Para la época que se publicó, tiene un estilo dinámico y poco verborrágico. Está lleno de acción, escenas memorables (no es raro que se hayan hecho tantas películas), personajes interesantes. ¡Y encima es un libro tecnófilo! No es una lectura forzada leer Drácula como una micro-expresión de la lucha entre la ciencia y la técnica positivista contra la cultura medieval reaccionaria, o cosas así.
Y entonces, cuando el otro día ví, en Work of Art (un reality), a unos diseñadores crear tapas para Drácula, se me ocurrió:
Editemos Drácula
Agarremos el original, que es de dominio público, hagamos una traducción moderna, hagamos ebooks, y quién te dice, una edición en papel. Regalémosle a la gente la posibilidad de leer una versión moderna de este libro buenísimo. Una traducción que no sea castiza, ni dé vergüenza hablando de "estofado con polvo de pimiento rojo" si no que diga goulash, o por lo menos "estofado con paprika".
Yo tengo un poquito de cancha haciendo typesetting de libros. Seguro que se puede conseguir ilustrador/a/es copado/a/as/os para la tapa, títulos (¡o para ilustrar intercalado!)
Y... lo mejor es que:
Es un libro relativamente corto
Está escrito en muchas voces distintas. No estaría buenísimo que Mina Harker lo escriba una mujer? Que Van Helsing escriba totalmente distinto que Lucy?
Entonces: se necesitan muchas cosas, pero más que nada, se necesitan traductores.
¿Quién quiere salir en la tapa de este libro? ¿Quién quiere traer al auténtico Drácula, el vampiro en serio, un auténtico macho de los cárpatos, de vuelta a la vida?
Anótense en los comentarios.