Name: [171] Florian Mayer
Member: 94 months
Authored: 7 videos
Description: I'm a student from Vienna, the federal capital of Austria. Please visit `My Blog <http://segfaulthunter.blogspot.com/>`_ ...

Decorators: Introducing functools.wraps [ID:887] (3/3)

in series: Advanced Python

video tutorial by Florian Mayer, added 09/08

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

functools.wraps is a function that allows you to take over the docstring and the name of another function when you make a decorator return another function than the one it takes.

import warnings
from functools import wraps

def deprecated(func):
    @wraps(func)
    def new_func(*args, **kwargs):
        warnings.warn("This function is old", DeprecationWarning,
                      2)
        return func(*args, **kwargs)
    return new_func

@deprecated
def i_am_old(foo, bar):
    """ This is a super-old docstring """
    return foo, bar

def change_doc(docstring):
    def decorator(func):
        func.__doc__ = docstring
        return func
    return decorator

#@change_doc("My super-new docstring")
def foo(bar):
    """ My old docstring """
    return bar

foo = change_doc("My super-new docstring")(foo)

if __name__ == '__main__':
    #print foo.__doc__
    print i_am_old(42, "Answer")

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 712 (since July 30th)
  • Plays in last week: 1
  • Published: 74 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

Thanks!


Also nice, but i didnt get the point of sense of this oO maybe it has something to do with my low knowledge about python? :D


5. anonymous Sun, 19 Jul 2009 20:48

Hi Florian,

Do you practice any rehearsals before you make a screen cast?

Preliminary Drawings?

Rough Drafts?

Have a co-worker look at what you are doing?

Run it by some users before publishing?

If something in the presentation goes wrong, can you not re-record it?

It is difficult for the viewer to know what you see as correct or incorrect results or the meaning of the results. We need your explanation of what we might expect to see and when shown something, what it is that we are looking at and what this result means to us.

I found this presentation extremely difficult to follow, it seemed like you were all over the place. I didn't understand what you were trying to accomplish, I didn't understand what it was that you were trying to convey. I didn't understand your example. I didn't understand the functions you pulled in. Then when some results weren't what you were expecting, I saw a lot of movement and heard a of clicking, but didn't know what was going on. It doesn't look like very much preparation was made before producing the screen cast. It definitively needs re-recording.

I think that one technique that might help you would be to have all the text typed out ahead of time and then in your presentation just go through highlighting various portions of the code, explaining, what each piece of the code does..

Thanks,

Lots of Luck,

Bruce

: ^ >


4. anonymous Sun, 03 May 2009 23:03

wrapper.__doc__ = wrapped.__doc__

wrapper.__name__ = wrapped.__name__

Before returning the wrapper looks more straightforward to me. (Also copy __module__ if you need to.) But maybe it's only me: I like to know what I'm doing =)


A little bit too quick towards the end (for my taste); I didn't know where to look first in order to retrace the shown, and thus didn't get it


great series thanks


The ShowMeDo Robot says - this video is now published, thanks for adding to ShowMeDo.


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Florian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com