Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Practical Maya Programming with Python

You're reading from   Practical Maya Programming with Python Unleash the power of Python in Maya and unlock your creativity

Arrow left icon
Product type Paperback
Published in Jul 2014
Publisher
ISBN-13 9781849694728
Length 352 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Robert Galanakis Robert Galanakis
Author Profile Icon Robert Galanakis
Robert Galanakis
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Introspecting Maya, Python, and PyMEL FREE CHAPTER 2. Writing Composable Code 3. Dealing with Errors 4. Leveraging Context Managers and Decorators in Maya 5. Building Graphical User Interfaces for Maya 6. Automating Maya from the Outside 7. Taming the Maya API 8. Unleashing the Maya API through Python 9. Becoming a Part of the Python Community A. Python Best Practices Index

Demystifying Python metaprogramming


To metaprogram is to write a program that writes or manipulates itself or another program. It sounds much more intimidating than it is. In fact, metaprogramming has been a large part of two chapters in this book.

We were metaprogramming in Chapter 6, Automating Maya from the Outside, with our use of eval and exec to run arbitrary code. The following is an example of metaprogramming using the eval function. We evaluate a string to sum the numbers from 1 to 5.

>>> s = '+'.join([str(i) for i in range(1, 6)])
>>> s
'1+2+3+4+5'
>>> eval(s)
15

We were also metaprogramming when creating closures and decorators in Chapter 4, Leveraging Context Managers and Decorators in Maya. The following is an example of metaprogramming using a function to return a different function.

>>> def _make_sort(reverse):
...     def dosort(items):
...         return sorted(items, reverse=reverse)
...     return dosort
>>> sort_ascending = _make_sort...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime