PyCon 2014

El uso del "import" en las aplicaciones Python

Allison Kaptur  · 

Presentación

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

all right let's welcome Allison captor and she's going to be presenting on important decisions is the audio okay here great I'm Allison captor I'm a facilitator at hacker school writers retreat for programmers in New York City so if you have

questions about hacker school come find me at any point in the weekend I'd love to talk to you about that I'm so with the 30 or so hacker schoolers who are here here are some places you can find me this is my github and Twitter if you don't get

a chance to get your questions answered at the end of this talk feel free to tweet at me and I'll try to get back to you there so I want us to picture ourselves in Amsterdam in 1989 inventing Python for the next half hour we are the BDF l's now unfortunately

I don't think we have time to invent all of Python in the next half an hour so we're just going to net one piece and that's import now our hypothetical here is that everything else in Python already exists so we can write code that we need to implement

import we're going to start with a very simple and very limited solution and then we'll gradually look at problems as they arise and try to solve them we'll also look at some other decisions that we could have made and see how other languages choose

to solve these problems as a side note import existed in it's essentially its current form in the first open source release of Python and around 90 91 so there's not a record of the decision making around the structure this is the days before mailing

list before peps before the changelog even so the main source for actual history of import is Guido's brain and I'll come back to that a little bit later on so let's stay in our problem really clearly this is one of sharing and reusing code so

to get some code up on the boat on the board here that we can talk about concretely suppose on the left hand side that we had written some function it was very hard work and it's a very useful function and sometime later on the right hand side we need

to use that function again so what are we going to do in the app sense of import well the first thing we could do is the decision that the novice programmer would make someone who's very new to programming they would say this is way easy I can copy and

paste now they're clearly a lot of disadvantages with the strategy but it's worth noting that there are advantages to for one thing it's very clear what we've done so if we're reading this function this file later a useful function is defined

right there we don't have to go hunting for it somewhere oh there are lots of disadvantages and two of them are that this is annoying to do by hand and if we subsequently find a bug in a useful function we have to go fix it in two places so at a minimum

we want an automatic solution that doesn't require manual copying and pasting that's probably not the best solution that we can come up with and we want the code to stay in sync if we make changes so let's imagine some kind of magical copy and

paste so we've got a special symbol that copies and paste your code for you and we pass it in a file name or something like that and we can imagine implementing this without any knowledge of Python at all basically that just views our source code as text

files and manipulates them as text files so we would look for this special symbol that gives us a file name we would go find that file and then replace the line in our the file writing today with the contents of that file that we found and right now this takes

entire files to include so we could choose to be more specific to say we only want this range of line numbers paste it into the file or writing today that has a lot of problems as well we would have to keep the line numbers and sync that's probably not

ideal but it's worth noting that this is an improvement we now have an automatic solution that's not manual copying and pasting and we don't have to fix bugs in two places our code stays in sync so there are lots of problems with the solution still

and we'll look at four of those over the course of the next 20 or so minutes for one thing this is static we can't change the behavior while the program is running for another we have name collisions if we have two things with the same name we can't

tell that that will happen we're executing our files multiple times which may be inefficient but has a much more serious problem of object identity and we'll spend a little more time on that later and it's very rigid there's current my only

[ ... ]

Nota: se han omitido las otras 2.187 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.