Decorator question
Chris
reilly.christopher at gmail.com
Wed Jan 26 21:37:57 EST 2011
More information about the Python-list mailing list
Wed Jan 26 21:37:57 EST 2011
- Previous message (by thread): Decorator question
- Next message (by thread): Decorator question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Jan 26, 6:17 pm, Chris <reilly.christop... at gmail.com> wrote: > I have a class (A, for instance) that possesses a boolean (A.b, for > instance) that is liable to change over an instance's lifetime. > > Many of the methods of this class (A.foo, for instance) should not > execute as long as this boolean is false, but should instead raise an > exception. > > Can I use a decorator to implement this functionality? More exactly, > could I define a function called 'checker' that accomplishes this: > > def checker(f): > ... > > class A(): > > b = True > > @checker > def foo(self,...): > print 'in foo' > > a = A() > a.foo() > a.b = False > a.foo() > > would result in: > > 'in foo' > Exception: ... > > This exact solution isn't necessary, just something that doesn't > require me to have the clunky: > > def foo(self,...): > if self.b: > ... > else: raise Exception('b attribute must be true before executing > this method') > > in every method. > > Thanks, > > Chris Sorry, discovered answer here: http://stackoverflow.com/questions/2309124/get-class-in-python-decorator
- Previous message (by thread): Decorator question
- Next message (by thread): Decorator question
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list