pymel.util.LazyLoadModule

static util.LazyLoadModule(name, contents)
Parameters:
  • name – name of the module
  • contents – dictionary of initial module globals

This function returns a special module type with one method _addattr. The signature of this method is:

_addattr(name, creator, *creatorArgs, **creatorKwargs)

Attributes added with this method will not be created until the first time that they are accessed, at which point a callback function will be called to generate the attribute’s value.

Parameters:
  • name – name of the attribute to lazily add
  • creator – a function that create the

Example:

import sys
mod = LazyLoadModule(__name__, globals())
mod._addattr( 'foo', str, 'bar' )
sys.modules[__name__] = mod

One caveat of this technique is that if a user imports everything from your lazy module ( .e.g from module import * ), it will cause all lazy attributes to be evaluated.

Also, if any module-level expression needs to reference something that only exists in the LazyLoadModule, it will need to be stuck in after the creation of the LazyLoadModule. Then, typically, after defining all functions/classes/etc which rely on the LazyLoadModule attributes, you will wish to update the LazyLoadModule with the newly-created functions - typically, this is done with the _updateLazyModule method.

Finally, any functions which reference any LazyLoadModule-only attributes, whether they are defined after OR before the creation of the LazyLoadModule, will have to prefix it with a reference to the LazyLoadModule.

Example:

import sys

def myFunc():
    # need to preface foo with 'lazyModule',
    # even though this function is defined before
    # the creation of the lazy module!
    print 'foo is:', lazyModule.foo

mod = lazyLoadModule(__name__, globals())
mod._addattr( 'foo', str, 'bar' )
sys.modules[__name__] = mod

# create a reference to the LazyLoadModule in this module's
# global space
lazyModule = sys.modules[__name__]

# define something which relies on something in the lazy module
fooExpanded = lazyModule.foo + '... now with MORE!'

# update the lazyModule with our new additions (ie, fooExpanded)
lazyModule._updateLazyModule(globals())

Previous topic

pymel.util.path

Next topic

pymel.util.abs

Core

Core Modules

Other Modules

This Page