LongCut logo

Pablo Galindo Salgado - Nobody expects the Spanish inquisition

By EuroPython Conference

Summary

## Key takeaways - **Python 3.10's improved error messages delight users**: Improvements to Python's error messages, inspired by user feedback and alternative interpreters, have been a significant and well-received development in Python 3.10, enhancing the user experience. [10:17], [11:44] - **Debugging CPython's complex bugs requires modularity and humility**: Debugging complex CPython issues involves breaking down problems into smaller experiments, checking assumptions, and maintaining humility, as bugs often reveal deeper misunderstandings of the system. [18:35], [24:46] - **Python's variable swapping idiom hides a stack-based execution model**: The Python idiom `x, y = y, x` for swapping variables doesn't use a temporary variable in the way C or Java does; instead, it relies on CPython's underlying stack-based execution model. [35:25], [38:51] - **Invisible forces drive system behavior, like dark matter in galaxies**: Just as dark matter invisibly influences galactic behavior, hidden factors like improved error messages can significantly drive user satisfaction and adoption of CPython versions. [08:31], [17:19] - **CPython's C implementation can lead to tricky memory corruption bugs**: The low-level nature of CPython's implementation in C can result in subtle memory corruption bugs, such as those caused by generic data structures mishandling types, requiring careful handling and safer internal structures. [21:28], [22:44]

Topics Covered

  • Python's Hidden Gem: The Power of Better Error Messages
  • Debugging: Uncovering Hidden Complexities and Learning Systems
  • How Black Formatter Once Made Your Python Code Slower
  • Unpacking Python's Magic: The Stack Machine Execution Model

Full Transcript

and now i'm going to pass over to pablo

pablo you probably know he's one of the

core developers he's the release manager

for python 3.10

and he's also a member of the steering

council that basically decides where

python is going in the next

well steering council is voted for every

year but it's basically decided where

python's development is heading

so i want to

hi pablo

hi

hi

you're here welcome to your python

thanks for having me

yes

very nice of you to come and then to

give your talk

so um i don't want to take more of your

time so i would suggest that you um

you start the the screen share

or the technicians put it up let's see

there you go

and uh i will stay around but i will now

leave the stage to you

thank you pablo okay

thank you thank you very much

okay um so thank you everyone for coming

uh i would like you also to take the

opportunity to thank the organizers like

organizing a conference this big is a

lot of work um i have organized smaller

conferences and i have uh ended

exhausted so uh goodness to all the

organizers and you know like make sure

to to say thank you to them if you have

the opportunity

um so uh and thank you for coming to

this keynote it's a bit early in the

morning but um let's see if we can get

something something interesting in this

for 45 minutes um so this is going to be

a bit special it's going to be a bit of

a journey over

like you know several aspects of um how

see python is developed and like how how

things work and core development um but

ideally um i would like also the kenya

to be around

an interesting idea that i had recently

and i i see this idea popping up

everywhere

um so i would like you to share to share

with you and hopefully i do not talk so

you are a bit convinced of of what i

want to see

okay so just a few comments over like

who i am because you know i'm going to

talk for uh 45 minutes so you probably i

want to know briefly why you should

listen to me for five minutes right um

so as max mentioned uh so i must supply

some core developer for several years

already uh i'm in the steering council

this year the steam calculator is kind

of this uh this group of cordless and me

people from the community that kind of

decides on um like peps and like

high governance something like that i'm

the python 310 and 311 release manager

as you know probably python 310 is like

months away we are super excited it's a

it's a fantastic release uh we think

it's going to be the best python ever

it's going to do faster has like super

shiny feature like pattern matching i

want to be very interesting stuff um and

in my daily work i i work in the python

infrastructure team at bloomberg um so

we are we are actually sponsor your

python so you have the opportunity uh go

and say hello uh into our digital booth

or whatever it's called these days um

cool um one interesting thing is that

before i work you know on python and you

know computer science and and whatnot um

i used to

research um black holes in academia

so uh

you know like this may sound like why is

this all black holes now right well but

bear with me it's an interesting

topic so let me tell you about uh just

just briefly about black holes because i

find them like super super interesting

and it's actually quite very related to

the the smaller story that i want to

show you so something that i used to do

when i used to study these things is to

produce this kind of simulation over how

black hole will look like a very similar

to the you know the the photo of the

black hole as you probably already see

my simulations are a bit different like

for instance this is a simulation over

how um a particular rotating black hole

uh distorts the the background so you

can see like the the dark part uh over

here is kind of the the even horizon

which is this region will like kind of

escape and you can see here like all

this distortion around like the the the

star field right normally these things

are kind of surrounded by like like

aggression this which is this kind of

like super hot matter that is falling

into it um and it has like this super

weird structure i don't know if you have

ever seen obviously this talk is not

really about black holes so i won't go

into a lot of detail uh but all this

thing here is kind of the aggression

disk uh the same thing as this one

inside like it's literally the same disc

um

like

being distorted by light if you cannot

see it in a kind of you make the camera

going around the black hole um to take

like you can also have like kind of

better images of course than just this

low res simulation but if you take the

the camera kind of like to look around

the black hole you can see how the kind

of the disk inside here becomes the disk

outside when it flips is quite quite

interesting right like there are these

super interesting beasts that destroy

space-time in in quite a dramatic way

right they create this kind of

space-time storms right okay so you know

like you could say okay so why black

holes so i wanted to look at this

picture uh this is kind of one of the

most interesting points of the talk so

what you're seeing here is like a photo

of from i think it's in 1997 until 2016

of the center of the galaxy and if you

see here this this like bright dots that

you see are stars um obviously you know

like the stars move quite slow like

these distance are like parsecs right

but the interesting here that you can

see is that uh for instance if you if

you're looking this star this star is

cycling something you see it's going in

circles like this but there is nothing

in the middle and this is basically the

question of like okay so okay if black

holes are black and they don't emit

light so how we know that they are there

right and this case here as you can see

it's a system right and this is going to

become very interesting in the talk it's

a system when

things are happening right you know

these bright stars are moving around um

but it seems that whatever is driving

the movement of these stars is invisible

right like we can see for instance as i

mentioned this one this is doing these

circles but this is nothing here there

is not a bright star in the middle right

like if you don't see it clearly there

is this other kind of you know

image that is more um

like a schematic when you can see the

movement of these stars as you can see

all these stars kind of are sequenced

something which is in the middle which

is not that right um which is a mystery

right like uh this is how one way that

we used to detect black holes like when

when you have like these massive stars

cycling around a single point which is

like black um then you can infer that

something is there and then you can

infer which is its mass and how much

volume that

does the mass is enclosing and then you

can kind of know okay so so you know

there is a black holder right um you

know you may be a bit confused like why

are you talking about this thing but

it's going to become quite interesting

uh okay on the top right so another kind

of interesting topic around like

mysteries of you know like like physics

and whatnot is is what we call dark

matter so when we look at galaxies and

then we see how fast these galaxies spin

like on different points of the disc

like for instance we see how how fast

they spin close to the center and how

fast the spin close to the edge um so

you can have this kind of plot right so

this this line over here is is what we

expect like the galaxy spins slower the

farther is from the center but it turns

out that what we see is this line over

here it's kind of the galaxy spins more

or less uh at the same speed which is

super weird right and this is

where it comes well with the thing that

we called our matter comes from right

like if you know it's kind of a weird

thing because it's like we are basically

saying okay so that matter is something

weird that instead of you know of

producing this result produce this

result if you don't have it clear

basically the game that we play is that

if you have this this problem like you

have you know one plus one equals three

and you know it doesn't it doesn't work

uh then you basically invent one extra

number and then you call this dark

number and then you're done that's

basically how physicists work right like

that is basically what we call right so

it's this entity that we don't know that

is there but the interesting thing is

that this dark matter in the galaxies

and the black holes in the images that

you saw are kind of driving the whole

behavior of the system but they are

invisible but you cannot see them but

this is one of the most important parts

in the system right it's literally

explaining how the system works and this

is going to become very important okay

so the question then is like okay so

what has you know black holes related to

python or to c python in particular

which is what this talk is about well

you could say that you know both uh

will uh absorb you if you are close

enough and will probably turn your soul

apart uh in uh you know in a rip of a

space time where not even like and can

escape

but you know like obviously

we are talking about two different

things um so what we are going to like

go now is to different aspects of like

you know see python development and

different interesting insights that

happen while we are you know developing

c python but we are trying to you know

the same way is playing with this black

hole and that matter idea of like

something visual is driving the system

um i i would like to kind of wrap around

with all these stories so all these

stories will have something in common

that hopefully we will see at the end um

but uh maybe you you will see the hints

as we go so the first like story or or

things that uh i want to talk about is

is kind of

the visual right it's okay well the same

way black holes are invisible there is

these things here that are invisible um

but what is invisible in c python

development so let me let me tell you a

story so this is something that happened

on twitter one day this year so anthony

for the ones that you don't know anthony

is a great member of the python

community that works on like a lot of

tools like flag aid uh like he's also by

the score developer like he's a super uh

nice person uh he tweeted this this

tweet right he said like oh you're

getting this kind of nonsense syntax

error on python right like it's horrible

horrible error um you can try the pi by

project right like bypass you know is

this a faster alternative interpreter is

fantastic you should try it out but it

turns out that he was complaining you

know like see python has these horrible

error messages well well by buyer has a

much better and you know i look at this

and say like

i mean you know challenge accepted

chinese i thought let me let me let me

try to fix and this tweet like was the

the start of like one of the biggest uh

new developments on python 310 which is

kind of this this new error messages so

let me let me kind of show you a bit

what is this around so for instance this

is one of the problems that we have in

mind that you have like a dictionary

right and you have a bunch of numbers

and the problem is that this dictionary

is not close it doesn't have a closing

bracket uh and then there is some other

call afterwards so it turns out that if

you run this example or python

on python 3 9 or three eight uh you will

receive this thing which is super weird

right it's telling you that this this uh

this equal sign in the in the uh some

other code example uh is invalid syntax

which kind of it is because you know

it's thinking that it's inside the

dictionary because he never saw it close

but like this is super confusing super

confusing so after some f4 and you know

quite a lot of changes on the token i

sort of see python which is one of the

oldest areas of the cold ways and it's

very very tricky as you can imagine one

cool thing that we have now in python

310 the same as pi by hash as well uh is

that now we can finally we can finally

point you in this particular example

that the problem is not like you know

there is there is an equal sign that is

scoring correctly is that this this open

bracket was was never closed um this

seems like a you know a very simple

thing right like but this is this is

super important because even like

experienced developers who know when

this normally can happen uh you know

their trip and you know this is this is

fantastic and this is this was actually

not super super easy to implement in our

particular tokenizer just because the

other happened after we pass over these

lines but we are super excited to have

this thing the same way as a piper

interpreter has and some others and this

is one of the examples right and then

you know then i say oh

you know i saw these two people and

people were like wow i finally like you

know this is this is something that we

all wanted like this is fantastic and

this this made me think like okay so we

may be on to something right like maybe

maybe you know people want the spiel and

you know people want like you know new

features and like pattern matching and

whatnot but maybe maybe this is

something that also people want this

these better messages right so turns out

that recently i've been working with

widow and lisandros on the new perk

parser which is the kind of uh this new

parser for python um that we introduced

in three python39 um there is two talks

about this person on this conference i'm

quite excited to see them um so so i i

will be there i encourage you to check

them um but the idea is that now this

parser is quite more powerful than the

one before and one thing that i've been

doing transforming and working with it

recently in python 310 is to make it

like much smarter regarding error

messages and error reporting which is a

bit complicated impact because pec

doesn't have the concept of a failure uh

but we have pulled it down and i'm quite

happy let me i want to show you some of

the examples that we have made so for

instance uh one thing that we do now is

that when there is a syntax error we

highlight now the whole range instead of

like just pointing to set we point to

the whole thing so in this example it's

a function called with the generator

without parenthesis so we tell you all

the things that are wrong here which is

quite cool before we only show you one

of the of the carrots uh something that

we do as well is that you know if you do

a conditional with only one equal here

so if something equals something uh

instead of telling you you know like oh

generic invalid syntax we tell you like

okay so you know maybe you mean like

equal equals instead of one equals which

is quite cool as well for instance this

is one of my favorites like if you have

a dictionary or a list or something and

you're missing here you're missing a

comma

then the interpreter will tell you oh

maybe you're missing a comma here right

which is quite cool instead of

complaining to the next line um

more things that we do which is quite

exciting like if you type uh an

attribute of a module or a class or

basically of anything else uh but that

is not there then python will try to be

helpful and tells you like oh maybe you

know maybe it's the name toppo that

doesn't exist you mean named apple

and so which is quite cool this also

works with local um local variables like

you know i was never probably many of

you are from germany so you probably can

pronounce washi much better than me

what a cool name ryla is virtually it's

one of the guys who discovered one of

the simplest black holes it means black

shield i would like to have like one

surname as cool as as this guy but i i

never remember how to spell it uh and in

this case you for instance if you use

you know you use the variable it will

tell you well i don't have any variable

with this spelling but maybe you mean

this other one which you know we are

super excited to

um to see and this is super cool right

um so so you know we keep adding these

things and then you know people keep

reacting like super well and say like oh

maybe we are into something like it

seems that people really really really

value ux experience and better messages

and this lead us to pipe 657 which is a

python 311 uh it's not on it's not on

python310 uh but this is a work that we

did with um amar and batu honteskaya and

the idea here is that now uh

the interpreter will tell you exactly

where errors happen in a line so for

instance here if there is some problem

because one of these values is known it

will tell you that the problem is in

this part of the expression right so

this operation is the one that is wrong

and um which is right because before it

was like super like imagine that you

don't have these lines over here telling

you exactly what's going on uh it would

be super difficult to know exactly

what's happening right and for instance

uh if it's known there is other other

kind of thing so you know i tweeted this

thing and i announced that we have this

thing and you know we had this like

fantastic response from the community

like like super engaging like it turns

out that everybody likes this thing like

it's it's this like i've been working on

so many features of c python like i've

been working on like some of the speed

improvements from python 3 109 11 uh

some of the new features um

you know some some of the typing stuff

but it turns out that that ux experience

is this thing that turns out that you

know like everybody really really likes

i mean obviously you can imagine that

like people like ux right like obviously

uh why do you want uh like better error

messages but like i was not expecting

this kind of response this is this is

one of the examples right like is this

this mysterious um like you know hidden

items that people

don't prioritize when they talk about

why they're going python normally they

say oh yeah i want a faster python right

or i want like you know type annotations

to do something or i want like pattern

matching but they they don't normally

discuss these things even if they really

really want them right so this is like

the invisible thing that is driving the

system like maybe what like make people

excited about python 310

is not that much pattern matching maybe

what makes people excited about python

310 is also obviously better matching is

exciting but you know maybe it's also

also the error messages right so so this

is an interesting thing to think about

like is there any other um like aspects

that people want that are hidden like

there is any more black holes here that

we don't see and it's driving the whole

system of what people like in python so

this is something that you know is

making me think a lot recently and and

it's one of the stories that i wanted to

share with you uh like how important

this this aspect is um you know we which

sounds cool but it turns out that

nothing is that simple right because you

know like okay so implement better

messages um so you know

if you look at how i was well i was

implementing these new error messages

because you know we needed to modify the

back parser to do the same and the the

back parser is is using back which is

another uh not especially good like

passion mechanism for making error

messages because it doesn't have a super

clear concept of error although you know

you can do it there is a lot of research

about this thing so basically this this

was my this was myself and the back part

right like the fingers here are

obviously backs and i was trying to

smash all the backs and the promise that

every time you know like you implement

some new error messages then the other

fail and you know it's quite quite

ridiculous so let me let me share one of

the weirdest bugs i i have seen while i

was developing these these better

messages

probably the weirdest one has been this

one so i type the same code in the rebel

three times this stupid for loop which

is totally valid and the third time the

parser says that it's invalid syntax so

it's like it's like a

like a quantum parcel right like

something like that the the for loop is

in a superposition of valid syntax and

invalid syntax and only when you look it

will collapse to another or either state

this was super ridiculous to find out

because you know like it's a

for loop that sometimes is invalid

something is not right and you know this

this took a lot of time because because

this is one of the kind of bugs that

happen after the fact uh like it's not

it's not like you know the back happens

and then something goes wrong like it's

something like you know the back happens

and then it goes through all the

interpreters like you know like all the

way through a compiler pipeline um and

at the end of the day uh turns out that

you know you get this invalid syntax not

a sectful

not um like crash

nothing it's just invalid syntax so so

what what is going on here like what was

happening so it turns out that um when

departure constructs the abstract syntax

tree which is this tree that basically

encodes uh before going to the compiler

it encodes the shape of your of your

program in a lexical in a in lexical way

so basically like for loop or maybe this

this is the target of the for loop and

et cetera so in see python the c in c

python is in c right a c language right

so c python is implemented in c and in c

we have kind of this this specific um

class basically right this is track and

we use this thing to store a collection

of something right so you know like it

has this size parameter here which is

how many elements do we have and then we

have this array of elements of void star

elements and then we have uh you know a

bunch of like macros here uh just to get

maybe an element uh then we have another

to set an element and then we have

another to the last line what is the

length of the sequence the problem is

that this thing is super generic like

you can store absolutely whatever like

you can store like like you know donuts

interiors flows whatever you want and

you will know what you have a store

because the parameter is boy star which

basically means whatever like you know

the c doesn't care so the problem is

that we were inserting into one of these

things something and then we were

retrieving it as if it was something

else so imagine that you put into a box

an orange and then you know i don't know

you close your eyes and then you

retrieve the orange and you pretend that

it's an apple i mean it's not going to

be like super super you know appley

right when you try to to bite it down

right so this was the problem it was

basically memory corruption

uh so when we were doing this thing we

were corrupting the memory but it was

valid memory so nothing was wrong from

the perspective of the c programming

language um but the problem is that you

know like the memory was getting

corrupted it was surviving the whole

interpreter uh it's just that like when

when uh it goes to to um

the time of passing the four uh the for

loop uh then it is basically not

recognizing that the back passer was not

recognized in the for loop because uh

one um one item that shouldn't have been

zero it was zero just because we were

corrupting this memory so the way we

solve this thing is that we i mean

obviously a more strong language uh like

c plus plus or russ or something like

that we'll have genetics which is how

you solve this problem but you see kind

of you don't have any of this so the way

we solve this thing is kind of like

trying to you know make this thing even

more complex and now we have this this

cool kind of sequences that you know it

has types here so for instance you can

have like now

okay so you have an array of python

objects and then there is kind of this

this crazy code here now that is able to

allow you to work with type information

which is still like you know

too complicated because we are

implementing genetics in c um but but

it's safer now now this kind of bugs

will not happen right so so but this was

super super tricky to find out right

like we we needed to you know think

about like how this could happen and you

know like like check all the memory that

was involved into these calculations so

you know i'm i don't know if you have

seen this kind of like like like table

about like you know like um

there is a name in english i never

remember for this thing alignment right

alignment charts for from dnd right so

you know like people pull here

programming languages maybe they say

okay python maybe it's love for google i

don't know

java is lovely i mean you know like all

programming languages are useful so this

is obviously a joke but something that

i'm super sure is that c is here like

you know

you kind of convince me like if you

program in c and i do like scale the

keyboard so that that's how it is right

like see it's just like a full gun

pointer to build your feet at the same

time it's a quantum photon um so c is

bad that's the conclusion

don't call it see if you can avoid it um

but let me tell you about other cool

stories and backs like um one

interesting thing about the bargaining

is is this um this sentence by uh

nicholas negro ponte that says like for

i mean it allows you to think about

thinking and while you debug and you are

learning um

you learn to learn it right and the idea

here is quite interesting because you

know like uh do you you need to you need

to be a bit the smarter that you were

when you write the call and when you

learn you are putting your assumptions

to test so the buying is probably one of

the most important tasks as a programmer

that you need to do right

because you need to

you need to have basically everything

that you learn and then you understand

to the system and almost by definition

something will be wrong uh so it's a

fantastic way to condense and check uh

very scientific as well right because

you do these little experiments and

whatnot um so i i really like the bag

not that much because of the sensation

because you know it's always frustrating

and backs are kind of hard and whatnot

but but it really really really puts all

your knowledge of the system to test and

it allows you to identify things faster

it allows you to be a bit humble because

it's kind of you need to find yourself

because you're going to be wrong like

that's that's why the bargain you have a

bag is like there is something that you

didn't understood um so you need to

fight yourself a bit and that's a super

interesting thing right like any systems

like see python it can be a bit complex

because you know like the box can be

super complex backs from outer space but

but i think it is one of the best

activities that you can do to learn as a

parameter

let me tell you about some other weird

bug that happened so for instance this

is a bug that happened on the fc python

test with we we have this kind of

comparison checker class that takes kind

of an object and every time it compares

equals to something it adds one to this

comparison uh field so basically it

counts how many times this object was

compared and then in the python test we

have this this this kind of test when we

were creating like like some other

object this could be a string or

something like that and then we were

comparing like creating this comparison

checker over a string so the compression

checker will check how many times this

string was compared to and then we

created this dictionary with both things

as keys

uh then we check if the compilation

checker with the same string is in x and

then we check how many comparisons right

like this this thing over here should

have created one comparison only because

you know it's comparing against the

string

um but it turns out that this was

failing sometimes only sometimes uh

which was super super annoying um like

it took us several hours to understand

what's going on because you know like

this thing was created only to be

compared once and it has been working

for years without nobody touching this

code and suddenly one afternoon it

crashes with like more than one

comparison so so you know like this was

quite hard because

you need to understand exactly how not

only how the dictionary is implemented

but also all the possibilities that can

happen there like for experienced python

developers you probably know what's

going on uh what's going on is that you

know like um

this thing can have a hash collision in

the dictionary and then you will compare

more times than one way to force a hash

collision here is to basically make the

other object that we were discussing has

the same hash as the string and here

what will happen is that once you add it

here then both a and b will be on the

same bucket inside the dictionary

because they will have the same hash but

there will not be equal so when you

check if the comparison check it is in x

then it will compare first to b and then

to a so the comparisons are not one or

two but how does happen like why this

was working under until one day so it

turns out that the strings in c python

uh has the hash being randomized so so

the highest of the string change every

time you open the interpreter with with

the bad luck that this some other object

here that we had which was just another

string maybe or i don't know what it was

but like a method a function or

something like that so the bad luck was

that that thing had a hash only on that

execution of the test suite that was

colliding for the first time in years

with the other string that we were

testing and for the first time in years

the number of comparison was not one uh

it tells you like something about

writing good tests i'm you know like uh

probably this was another side but you

know the bargain this thing was quite

weird because you are expecting to see

something like threats and like you know

it's a race condition only happens

sometimes but this was just applying

dictionary it looked like super

deterministic but it turns out that it

was not that deterministic right like

you know it can be it can be hard you

need to when you're dividing things

especially as a python you need to be

thoughtful of what's going on um let me

then go to some some other event here so

um so the idea here that that you know

some insights on the bargain is that um

one interesting thing that you can do

when you are starting to develop

something that you really don't

understand like you know think about

these um super complex backs that you

see that where no nothing like seems to

make sense so one one idea here that i

normally do when i the back is that um

so i try to be modular the idea here is

that you don't try to change uh multiple

things at the same time right like you

know just do very very small things

almost like you know like the scientific

method like to do a little experiment

you see what happens but if you start to

change like multiple things at the same

time even if it's very tempting because

you know maybe you're changing like an

equal equals and a less than or

something like that because you think

both are wrong try to do one by one and

see what happens because otherwise

you're going to you're going to probably

be in non-linear behavior and you really

really want to see like the facts of

everything that you think is independent

so you know take your time debugging is

a task that you know takes it sometime

uh and if you try to rush it you

probably

you know will

not be able to succeed very well so you

know try to do these little changes

another interesting thing is that you

know check your assumption and the idea

here is that um one of the most cool

things that i normally do here is to

bring to the problem someone who has

never worked with that part of the code

base like you can see python or maybe

also like you know if you are debugging

your company or your own code so try to

because these people have super powers

right like these people will see the

system for the first time so you won't

have your biases that you have towards

it right they will tell you like oh yeah

but this thing that you say it doesn't

make sense right maybe you you have the

thing already integrated and then you

say yeah this is this why right but

maybe these people will tell you i i

don't understand like what this is is

right right um then the the next thing

is like you know try to break break down

the thing right like the idea is that

there is some super mysterious thing

right like if you remember this this

this mysterious things that are driving

the bike maybe they are not the back but

they are driving the back this is what

makes the bug happen this is the black

hole of the bargain right the mysterious

invisible force that is dominating the

system but nobody sees so the idea is

that you don't you don't go you know

head on towards the black hole it's a

bad idea right it's going to is going to

trap you forever so you need to kind of

break it down right like you you start

saying like okay so what is the next

thing that i don't understand like can i

can i do like a small experiment to

understand what's going on and like you

know it's not a super smart like advice

because probably you're already doing it

but like you know try try to always

think about like small problems don't

try to like like you know uh grab the

the big problem and all ahead and the

the next like advice is like this lay

hypothesis like try to not make a

hypothesis at the beginning of the of

the the program because then you're

going to go into confirmation bias like

you're going to say

you think oh i think this is what is

happening and if you don't have any

enough like information then what's

going to happen is that every every

behavior that you see from the system

you're going to try to justify towards

your hypothesis right and maybe what

happens is that you are convinced enough

which is one of the most dangerous state

to be in and then what you end doing is

like you're putting these partial fixes

or hacks right just because you are

convinced enough and then you say this

seems to fix the problem or even i don't

understand why so let's go ahead right

um so you know this is like a small

advice that i have learned over the

years of the vacancy python and i think

it will be interesting for you let me

tell you about another cool thing right

like julie evans has a lot of

discussions um on the back end and she

has this fantastic sentence that says

like she loves the backing box because

you know uh it means that you're going

to learn something new which is

something i always say when i have like

some super weird back happening like it

means that i have not understood

something on my system or on c python

and i'm going to learn something new so

you know like at least you have that to

um to have in mind we'll do the back the

back let me tell you some some uh one of

the funniest bugs that we have in c

python it's is this dpo 34 something

something right so this this this back

over here is samsung back on on freebsd

um so this bag took like six days to

solve just because we needed to like go

through to an rfc and find out like you

know like like how the network layer

like was supposed to behave we needed to

understand like you know how the packets

were able to behave on different linear

systems we found this sentence on some

forgotten page of the rsc that says uh

or portable applications like

applications that can be making

different operating system needs to be

uh providing some particular thing so

after six days this was appear it was

like a one-line change this is six days

of the bargain right like just just to

show you that this this can happen when

you are doing something as complex like

not working but probably my favorite

debug in c python is this one is

bpo37213

people optimizer does not optimize

functions with multiple expressions uh

but i like to call this back how back in

c python made call format with black

format that is lower so let me tell you

about the problem so imagine you have

this list comprehension you have this x

for x in interval if x right this is the

byte of the generates right it's going

to build at least it lost uh like the

interval that we store in this secret

variable called dot zero and then it

lost the x to see if it is true or not

and if it's true it goes to false to the

next item and if it's not true uh it

lost the x and appends it to the list

right and then it jumps to the next one

if you see here there is two jumps here

there is the jump in number 10 and the

jump in number 16 both jump to the next

one on number four right so so you know

when ball jumps will go to the to the

same place but if you break the list

comprehension in two lines or three

lines then the y code is mysteriously

different and the difference is that

this jump over here instead of going

directly to four which is the next item

in the least comprehension it goes to 16

which is another jam then then goes to

four which means that instead of like

you know when when it sees that there is

an item here and this is there all of

these false instead of going directly to

the next item it goes to another

bytecode that then they goes to the

four-eater this is called like jump

elimination when this doesn't happen but

it turns out that we weren't doing this

thing uh with uh with with a certain

kind of code right a certain kind of

jobs an interesting thing here is that

you know like

this is just another code but if you

have a super big leak comprehension of a

loop this can make your parameters lower

and you know think about a tool that

converts very long lines uh leading

comprehension in one line into like you

know like like comprehensions spawning

multiple lines so basically because the

code in the bottom was slower due to

this back so black

could have been seen as that the

optimizer like it was it was formatting

your color making it more beautiful but

also making it slower obviously the bag

is fixed on black is not doing nothing

anymore but i always find it very funny

that that black was basically the first

python and optimizer um you know

interesting thing so let's go to the to

the kind of like the last like

interesting insight that i have so this

this revolves to a story that i i saw

one day when when you know like in some

python meetup in spain

and uh the idea is that uh it was this

uh this person that was confused by a

python idiom so this is the python idiom

this person was confused because this

person came from ceo from java and like

who knows right and the idea here is

that you know they were seeing this code

to swap variables you know x comma y

equals y comma x which we all you know

we all call in python we love and we

understand and it's one of the most

expressive things in the language uh but

maybe not one of them but like you know

it's a very expressive thing and but

this this person was like super confused

like why there is no temporary variable

like how does this even work because in

c or in java or whatever this person was

calling before uh this person was used

to have like a temporary variable that

was swapping both right

like and and the answer to the person

like that this guy was asking like why

is this happening it was like what's

going on here is double unpacking and

that was explanation it's staple and

packing and and you know the person

obviously that didn't understood the

call uh so was was given the answer and

the answer is staple i'm packing and you

know like that is my point here and the

whole the whole thing that i wanted to

teach you here is that apple unpacking

was the horriblest answer in the world

why is this because like what is even

temple unpacking right like do you have

a person who is not asking a question or

like you know like

how is how is this called it's not how

is this called it's like how is this

possible and you know this person have

uh like

is the question is hinting like okay so

so how is this implemented because in my

language in c or java or whatever i need

a temporary variable and here i don't

need a temporary variable and you know

if you throw like a word and this

happens you know to a lot of us right

like you you are maybe on your or asking

some of your colleges or like you know

some person and they give you this like

like you know like names ryan super

complex names and that is the

explanation this is a blue blue blue and

like you know that is another cool

answer and the reason is because this is

stopping you from going deeper like to

understand what's going on so if this

person who didn't understood like x

comma y equal y comma x instead of like

you know receiving

people and packing as an answer that

person will have like going deeper and

say okay so i have like this code so

what happens with this code like what

what what is going on is like the

question is okay so so this is like

python code which is obviously going to

be translated into bytecode and it's

going to be executed on c python c

python is c i mean c you need a

temporary variable to do something like

so the question one interesting question

that you can actually answer ask

yourself like

is there a temporary variable being used

by c python here internally like a magic

thing right like for us the python does

this this nice thing um so it's a

temporary variable or not and that is a

super interesting question so let's try

to find out so when you do this code it

executes to bytecode right and the

biblical instructions for this

particular operation is a

operation called rotu which means rotato

which is basically like you know swap x

and y so let's see uh you know how that

will look like of course like in c you

will do this thing right like you will

have like uh like x x and y and you know

if you want to put x where y is you will

need like a temporary variable so i will

say okay let me save the status of x in

my temporary then let me assign to y and

then let me recover y this is what the

person was expecting to but this is this

is like the question is like easy python

doing something like this internally yes

no let's see what's going on so what's

going on you know like when you like

check the vehicle this you will see that

the bike is is like you load the

variable x you know sorry that variable

y you know the variable x then you

execute this mysterious instruction

called rod row two like you know rotate

two and then you store x and then just

throw wire right so it's kind of like

you

stay some mysterious rotation and then

you do some stores so it's like it's

this rotation on these two stores using

a temporary so let's see let's see how

these two are implemented so this is the

by the c code for raw tube so

you see it's super easy what you do here

is that you take one

one object from the top of your stack

then you take another object for the top

of your tag and then you put them in the

stack in the opposite order there is not

a temporary variable i mean you could

consider that these locals here could be

temporary variables but they are not in

the same sense as this temporary

variable that we need in the other

example right you could store this thing

on the stack itself so you don't need to

see temporary variables or and actually

if you check if you check the assembly

this is actually going to be storing

registers so there is no temporary

variables per se right i mean you could

also argue that in the in the temporary

variable is going to be a register there

but

my point here is that

here a confusion over like you know how

the swap works shows you something much

much more interesting which is that the

execution model is different the

execution model here is a stack what you

have is a stack with the vario like you

know if you have never seen this thing

it's quite confusing but you know you

can you can google it right what you

have is a stack and then you have like

the variables there and to just swap two

variables on my stack i just pull them

from the stack both like i take the top

and the on the second and then i put

them in the stack in the opposite order

and that basically swaps the thing like

if instead of x plus y i want to do y

plus x i will take x i will take y and

then i will put y and then i will put x

and or the other way right and then the

idea here is that that this is a much

more interesting thing if this person

that was confused by the double

unpacking answer will have like go a bit

difference like so so why i'm confused

like why is the thing that i don't

understand what is the black hole in

this system the invisible thing that is

driving the whole thing you will have

understood that you know like there is a

new thing that maybe you knew that is

that c python in particular is using a

stack machine as a as a you know as an

execution model

you could also keep going deeper that is

like deeper as much as you want you

could say okay so this is c

but what happens like when this c is

compiled to assembly so what happens is

this thing this is the assembly code

that is being generated which is like

it's like a bunch of moves right like if

you check what's going on is that the

every two moves is basically one of

these instructions here and what you see

here is that all of these are basically

using like like two registers right plus

offset right it's using the rbp register

and the racks register you don't need to

understand this code the point here is

that uh what you need to know here is

that you know assembly is a register

race machine which also has a stack so

you could also say like okay so you know

my code uh that swaps two variables

which is implemented in python is

transferred to bytecode which is derived

by c using a stack that's why i don't

need a temporary variable uh but then

you could say there is a temporary

variable in the sql and then you check

okay so that is translated then to

assembly and is this this bunch of movie

instruction which is just the movie

instruction is basically moving things

from memory although you know it turns

out that this thing is too incomplete by

the way like you can absolutely do a

compiler that only meets movie

instructions some person has done that

actually it's called the mofu skater

it's quite crazy like how powerful just

moving things memory can be um so in

particular this code is just like a

bunch of more instructions um but then

you can you can discuss like okay so you

know there is a temporary here and it

turns out there is another execution

model which is totally different here

which is you know like a registered

stack machine and you could think that

the register is temporaries or not so it

is now up to you to decide if at the end

of the day it was or not a temporary

variable but you know a person who have

the time to go deeper and like you know

maybe ask questions to people and

understood it will have discovered all

this beauty right all these execution

models like i don't know you have seen

this meme but um is the

like the python programmers saying like

okay so it's x plus y all an abstraction

and you know like like c plus plus and

everyone's like yeah always have been

right and maybe you can even go to

transistors if you have like some some

some time or a

physics or electrical engineering um

you know um it's interesting you can

keep going but like going deeper is

something fantastic because it's

something that will tell you um

[Music]

uh like like the behavior of your system

right um so so i wanted to finish just

does the talk right and the idea here is

that in the same way you know the the

black hole was was the mysterious thing

and the dark matter here was the

mysterious thing driving the whole

system of these stars this these

examples that i showed you are things

that you know are hidden um at the

beginning but they are driving the whole

thing and this is the thing that you

need to do right like you need to you

need to seek the dark matter in all

these examples to to have a better

understanding um and i think you will be

a much better um professional and a much

better engineer if you lose you seek

this invisible forces right and that's i

think i i hope i have not born a lot of

extra time um so that's that's uh the

finish of my talk i hope you have

enjoyed it

thank you very much pablo that was an

excellent talk very exciting very

very deep as well

unfortunately we we are running out of

time so we cannot answer any questions

uh in this

main room but i would suggest that you

perhaps go to the breakout room optiver

on matrix and then you can you can you

know answer questions there i'm going to

copy the the questions

that i collected to that room and then

you can have a look there

so everyone is very excited

you're getting a nice applause

thank you

nice

yeah we're kind of trying to stimulate

everything right that is very neat in

this time

okay so um yeah i very much enjoyed your

talk thank you for for uh coming to the

conference thank you for the keynote and

i hope you enjoy the rest of the

conference so thank you so much

we will now have a short break and then

we will head on to the next session bye

bye

bye

Loading...

Loading video analysis...