Skip to main content

Ralsina.Me — Roberto Alsina's website

The problem is is. Is it not?

This has been a re­peat­ed dis­cus­sion in the Python Ar­genti­na mail­ing list. Since it has not come up in a while, why not re­cap it, so the next time it hap­pens peo­ple can just link here.

Some peo­ple for some rea­son do this:

>>> a = 2
>>> b = 2
>>> a == b
True
>>> a is b
True

And then, when they do this, they are sur­prised:

>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False

They are sur­prised be­cause "2 is 2" makes more in­tu­itive sense than "1000 is not 1000". This could be at­trib­uted to an in­cli­na­tion to­wards pla­ton­is­m, but re­al­ly, it's be­cause they don't know what is is.

The is op­er­a­tor is (on CPython) sim­ply a mem­o­ry ad­dress com­par­i­son. if ob­jects a and b are the same ex­act chunk of mem­o­ry, then they "are" each oth­er. Since python pre-cre­ates a bunch of small in­te­gers, then ev­ery 2 you cre­ate is re­al­ly not a new 2, but the same 2 of last time.

This works be­cause of two things:

  1. In­­te­gers are read­­-on­­ly ob­­jec­t­s. You can have as many var­i­ables "hold­ing" the same 2, be­­cause they can't break it.

  2. In python, as­sign­­ment is just alias­ing. You are not mak­ing a copy of 2 when you do a = 2, you are just say­ing "a is an­oth­er name for this 2 here".

This is sur­pris­ing for peo­ple com­ing from oth­er lan­guages, like, say, C or C++. In those lan­guages, a vari­able int a will nev­er use the same mem­o­ry space as an­oth­er vari­able int b be­cause a and b are names for spe­cif­ic bytes of mem­o­ry, and you can change the con­tents of those bytes. On C and C++, in­te­gers are a mu­ta­ble type. This 2 is not that 2, un­less you do it in­ten­tion­al­ly us­ing point­er­s.

In fac­t, the way as­sign­ment works on Python al­so leads to oth­er sur­pris­es, more in­ter­est­ing in re­al life. For ex­am­ple, look at this ses­sion:

>>> def f(s=""):
...     s+='x'
...     return s
...
>>> f()
'x'
>>> f()
'x'
>>> f()
'x'

That is re­al­ly not sur­pris­ing. 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 some­one who has not seen it be­fore, sur­pris­ing. It hap­pens be­cause lists are a mu­ta­ble type. The de­fault ar­gu­ment is de­fined when the func­tion is parsed, and ev­ery time you call f() you are us­ing and re­turn­ing the same l. Be­fore, you were al­so us­ing al­ways the same s but since strings are im­mutable, it nev­er changed, and you were re­turn­ing a new string each time.

You could check that I am telling you the truth, us­ing is, of course. And BTW, this is not a prob­lem just for list­s. It's a prob­lem for ob­jects of ev­ery class you cre­ate your­self, un­less you both­er mak­ing it im­mutable some­how. So let's be care­ful with de­fault ar­gu­ments, 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.

Test­ing for in­te­ger iden­ti­ty is like wor­ry­ing, af­ter you loan me $1, about whether I re­turn you a dif­fer­ent or the same $1 coin. It just does­n't mat­ter. What you want is just a $1 coin, or a 2, or a 1000.

Al­so, the re­sult of 2 is 2 is im­ple­men­ta­tion de­pen­den­t. There is no rea­son, be­yond an op­ti­miza­tion, for that to be True.

Hop­ing this was clear, let me give you a last snip­pet:

>>> a = float('NaN')
>>> a is a
True
>>> a == a
False

UP­DATE: lots of fun and in­ter­est­ing com­ments about this post at red­dit and a small fol­lowup here

Hello

Hel­lo, my name is Rober­to Alsi­na. You may know me from my ap­pear­ances in "KDE De­vel­op­ers in the Stone Age" and "PyQt pro­gram­mers gone wild".

On the oth­er hand, I am not:

  • Rober­­to Al­si­­na, who lives in Hous­­ton and is ac­­tive in the Ar­­gen­­tine ex­­pa­tri­ate com­­mu­ni­­ty.

  • Ama­­do Rober­­to Al­si­­na, paraguayan poli­ti­­cian.

  • Rober­­to Ariel Al­si­­na (a­­ka Rober­s­tor­m) who drives a mo­­tor­­cy­­cle.

  • Rober­­to Al­si­­na, puer­­tor­i­­can ar­chi­tec­t.

  • Rober­­to Al­si­­na, who lives in Vil­la Ale­m­ana and at­­tend­ed Cole­­gio Buck­­ing­ham.

  • Rober­­to Al­si­­na Ruibal, who I hope is wear­ing a wig.

  • Rober­­to Gon­za­­les Al­si­­na, who lives in Canelones. I on­­ly eat canelones.

  • The FEARED Rober­­to Na­­va Al­si­­na who, while on­­ly 15 years old, is scor­ing goals in some am­a­­teur league some­where.

  • Rober­­to Le­bron Al­si­­na (I have slight­­ly more hair)

  • Rober­­to An­­to­nio Gómez Al­si­­na, ac­­tivist against bul­l­­fight­­ing

Hope­ful­ly, this will clear things up. Thanks for read­ing.

Re-Editar Drácula: Proyecto que me gustaría que alguien agarre.

This post makes no sense in en­glish, so span­ish on­ly!

Drácu­la es un li­bro muy par­tic­u­lar. Casi to­do el mun­do cree que sabe de qué se trata, pero en el 90% de los ca­sos no es así. O sea, sí, saben que es de un vam­piro, blabla.

Lo que no saben es na­da del li­bro. Saben de las pelícu­las, del es­pe­cial de Scoo­by Doo, de­los chistes de vam­piros, y cosas así, pero el li­bro en sí, no lo han leí­do.

¡Y es una lás­ti­ma! Es un li­bro muy in­tere­sante. Para la época que se pub­licó, tiene un es­ti­lo dinámi­co y poco ver­bor­rági­co. Es­tá lleno de ac­ción, es­ce­nas mem­o­rables (no es raro que se hayan he­cho tan­tas pelícu­las), per­son­ajes in­tere­santes. ¡Y enci­ma es un li­bro tec­nó­filo! No es una lec­tura forza­da leer Drácu­la co­mo una mi­cro-­ex­pre­sión de la lucha en­tre la cien­cia y la téc­ni­ca pos­i­tivista con­tra la cul­tura me­dieval reac­cionar­i­a, o cosas así.

Y en­ton­ces, cuan­do el otro día ví, en Work of Art (un re­al­i­ty), a un­os dis­eñadores crear tapas para Drácu­la, se me ocur­rió:

Edite­mos Drácu­la

Agar­remos el orig­i­nal, que es de do­minio públi­co, hag­amos una tra­duc­ción mod­er­na, hag­amos ebook­s, y quién te dice, una edi­ción en pa­pel. Re­galé­mosle a la gente la posi­bil­i­dad de leer una ver­sión mod­er­na de este li­bro buenísi­mo. Una tra­duc­ción que no sea cas­ti­za, ni dé vergüen­za hablan­do de "esto­fa­do con pol­vo de pimien­to ro­jo" si no que di­ga goulash, o por lo menos "esto­fa­do con pa­prika".

Yo ten­go un po­quito de can­cha ha­cien­do type­set­ting de li­bros. Se­guro que se puede con­seguir ilustrador/a/es co­pado/a/as/os para la tapa, tí­tu­los (¡o para ilus­trar in­ter­cal­ado!)

Y... lo mejor es que:

  1. Es un li­bro rel­a­ti­­va­­mente cor­­to

  2. Es­­tá es­­crito en muchas vo­ces dis­­t­in­­tas. No es­­­taría buenísi­­mo que Mi­­na Hark­er lo es­­cri­­ba una mu­­jer? Que Van Hel­s­ing es­­cri­­ba to­­tal­­mente dis­­t­in­­to que Lucy?

En­ton­ces: se nece­si­tan muchas cosas, pero más que nada, se nece­si­tan tra­duc­tores.

¿Quién quiere salir en la tapa de este li­bro? ¿Quién quiere traer al autén­ti­co Drácu­la, el vam­piro en se­ri­o, un autén­ti­co ma­cho de los cár­patos, de vuelta a la vi­da?

Anótense en los co­men­tar­ios.


Contents © 2000-2023 Roberto Alsina