Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Advanced decorator topics


Let's go over a few pieces of advice concerning decorators that did not fit into earlier sections.

Defining decorators with arguments

Decorators with arguments are an advanced topic. They can be implemented as types with a __call__ method, or as a triple-nested function. For example, the following two pieces of code behave the same.

def deco_using_func(key):
    def _deco(func):
        def inner(*args, **kwargs):
            print 'Hello from', key
            return func(*args, **kwargs)
        return inner
    return _deco

class deco_using_cls(object):
    def __init__(self, key):
        self.key = key
    def __call__(self, func):
        def inner(*args, **kwargs):
            print 'Hello from', self.key
            return func(*args, **kwargs)
        return inner

Either one would be hooked up as follows. Note that the decorator is called with arguments.

>>> @deco_using_func('func deco')
... def decorated_func1():
...     return
>>> decorated_func1...