The Artima Developer Community
Sponsored Link

Weblogs Forum
Decorators I: Introduction to Python Decorators

34 replies on 3 pages. Most recent reply: Sep 11, 2014 6:32 PM by Gary Mason

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 34 replies on 3 pages [ « | 1 2 3 ]
Dan Breslau

Posts: 1
Nickname: dbreslau
Registered: Jul, 2010

Re: Decorators I: Introduction to Python Decorators Posted: Jul 6, 2010 10:50 AM
Reply to this message Reply
Advertisement
@Matthew H: This is probably too late to help, but the solution is to add a __get__ method to your decorator class, making it a descriptor:

import types
# ...
def __get__(self, obj, ownerClass=None):
# Return a wrapper that binds self as a method of obj (!)
return types.MethodType(self, obj)

For details, see (Self-promotion alert!) http://www.outofwhatbox.com/blog/2010/07/python-decorating-with-class/ or Ian Bicking's post: http://blog.ianbicking.org/2008/10/24/decorators-and-descriptors/

andrej panjkov

Posts: 1
Nickname: mataap
Registered: Aug, 2011

Re: Decorators I: Introduction to Python Decorators Posted: Aug 4, 2011 10:35 PM
Reply to this message Reply
> > Is it possible to
> > decorate func1 without having to edit its source file?
>
> The @deco decoration is just gives a visual clue for the
> reassignment foo = deco(foo) that has to be
> written after foo was defined. So it's possible to load a
> module and decorate each of the functions without prior
> manipulation of the source.

Can someone illustrate this? If I import a module of functions at the console, how can I use a decorator to wrap one of the imported functions with say a timing decorator?

David Mahler

Posts: 1
Nickname: dmahler
Registered: Jun, 2012

Re: Decorators I: Introduction to Python Decorators Posted: Jun 9, 2012 11:29 AM
Reply to this message Reply
Thanks for posting this. As someone new to Python this was much more to the point than other sources that spend more time debating the use and the history of decorators than even explaining concrete examples.

monica p

Posts: 1
Nickname: monicathri
Registered: Mar, 2013

Re: Decorators I: Introduction to Python Decorators Posted: Mar 25, 2013 8:10 AM
Reply to this message Reply
how to disable warnings in python using decorators?

Gary Mason

Posts: 1
Nickname: masong
Registered: Sep, 2011

Re: Decorators I: Introduction to Python Decorators Posted: Sep 11, 2014 6:32 PM
Reply to this message Reply
IMHO, there should be a discussion and examples of decorating classes. There should be at least some mention of the special built-in decorators.

Flat View: This topic has 34 replies on 3 pages [ « | 1  2  3 ]
Topic: A Hazard of Covariant Return Types and Bridge Methods Previous Topic   Next Topic Topic: For the Sake of Simplicity


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us