PyCon 2014

Cómo crear "decorators" avanzados en las aplicaciones Python

Graham Dumpleton  · 

Presentación

Vídeo

Transcripción

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

I hope you enjoyed your first full day of talks last talks a lot of this room I'd like to present Graham Dumbleton who generously offered to fill the slot on very short notice he's the author of some software you may have once he was called mod WSGI

and he is currently a senior engineer at New Relic and he's going to be speaking to us tonight about advanced methods for creating decorators let's get more on welcome okay before I get started this is this is a talk I have done before which is why

I've managed to just drop into the spot pretty quick I presented it New Zealand Python last year I had put dinners a proposal for this conference but I didn't get accepted and if you followed my blog you would have known I did a huge blogpost series

at the beginning of the year about this topic so it's obviously something I'm quite passionate about and it's an interesting topic the reaction you get from it because I sort of go way beyond the normal way that people usually write decorators

I sort of well when I'll lose people in what I'm describing because it's sort of much more complicated in what they were expecting and I get her a lot of reactions of why do I care about all this detail why do I need to know the way I do it now

works well I don't quite think it does which is part of it what am i explaining so the thing to take away from it a guess is if you if you don't quite understand it what I think is really good about what I'm doing in this and what I doing in the

blog is that I'm not doing what a lot of people do and they they talk about a topic like decorators you'll get a lot of blog posts they say hey look I went and understood decorators on the weekend this is what they are and I'll explain in a few

paragraphs and I think that's all that's to it I like to go beyond that and I like to create a story about how I got to that solution of what for something from how it works so if you this is something like if you ever do blog posting yourself I really

really encourage you to do that don't just say oh I work this out this a solution describe the journey you went on to get there because it's really interesting to read that when people come along and do it later and if you take anything away from that

if you don't understand what I'm talking about please at least do that and I hope you enjoy my story on this one so I should mention that obviously Colton Meyers did to talk this morning on decorators so he's covered a lot of really basic stuff

here but most of what he was talking about sort of that's my first three or four slides and then I get into the heart of stuff so this is just a quick recap of what Colton was talking about so even if you've never written your own decorator and I've

only used them you would know that decorators from from the app symbol used to prefix their usage the app symbol here though is little more than syntactic sugar one can do the same thing invoking the decorator function explicitly parking passing in the function

to be decorated and replacing the original with the result in fact this is what you had to do before the decorator syntax was introduced in Python 2.4 the decorator syntax is therefore just a shorthand way of being able to apply a wrapper around an existing

function or otherwise modifying the existing function in place while the definition of the function is being set up the more illustrative way of showing how a wrapper works is to implement using a class object the class instance is initialized with and remembers

the original function object when the now wrapped function is called it is actually the dunder score call method of the wrapper object which is invoked this in turn would then call the original wrapped function a pass through wrapper isn't particularly

useful so normally you would actually want to do some work either before or after the wrapped function is called or you may want to modify the input arguments or the result as they pass through the wrapper using a class to implement the wrapper for a decorator

isn't actually that popular instead a function closure is more often used this case a nested function is used as a wrapper and is that which is returned by the decorator when the now wrapped function is called the nested function is actually being called

this in turn would again call then call the original wrapped function in this situation the nested function doesn't actually get passed the original wrapped function explicitly but it will still have access to it via the arguments given to the outer function

call this does a way of the need to create a class to hold what was the wrapped function and thus why it is can convenient and generally more popular now when we talk about functions we expect them to specify properties which describe them as well as document

what they do these included underscore name and underscore doc attributes when we use a wrapper though this no longer works as we expect as in the case of using a function closure the details of the nested function are returned if we use a class to implement

the wrapper as class instances do not normally have ADD underscore name attribute attempting to access the name of the function will actually result in an attribute error exception the solution here when using a function closure is to copy the attributes of

interest from the wrapped function to the nested wrapper function this will then result in the function name and the documentation strings been correct needing to manually copy the attributes is laborious and would need to be updated if any further special

[ ... ]

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