Package param :: Module parameterized :: Class Parameter
[hide private]
[frames] | no frames]

Class Parameter

source code



An attribute descriptor for declaring parameters.

Parameters are a special kind of class attribute.  Setting a
Parameterized class attribute to be a Parameter instance causes
that attribute of the class (and the class's instances) to be
treated as a Parameter.  This allows special behavior, including
dynamically generated parameter values, documentation strings,
constant and read-only parameters, and type or range checking at
assignment time.

For example, suppose someone wants to define two new kinds of
objects Foo and Bar, such that Bar has a parameter delta, Foo is a
subclass of Bar, and Foo has parameters alpha, sigma, and gamma
(and delta inherited from Bar).  She would begin her class
definitions with something like this:

class Bar(Parameterized):
    delta = Parameter(default=0.6, doc='The difference between steps.')
    ...
    
class Foo(Bar):
    alpha = Parameter(default=0.1, doc='The starting value.')
    sigma = Parameter(default=0.5, doc='The standard deviation.',
                      constant=True)
    gamma = Parameter(default=1.0, doc='The ending value.')
    ...

Class Foo would then have four parameters, with delta defaulting
to 0.6.

Parameters have several advantages over plain attributes:

1. Parameters can be set automatically when an instance is
   constructed: The default constructor for Foo (and Bar) will
   accept arbitrary keyword arguments, each of which can be used
   to specify the value of a Parameter of Foo (or any of Foo's
   superclasses).  E.g., if a script does this:

       myfoo = Foo(alpha=0.5)

   myfoo.alpha will return 0.5, without the Foo constructor
   needing special code to set alpha.

   If Foo implements its own constructor, keyword arguments will
   still be accepted if the constructor accepts a dictionary of
   keyword arguments (as in ``def __init__(self,**params):``), and
   then each class calls its superclass (as in
   ``super(Foo,self).__init__(**params)``) so that the
   Parameterized constructor will process the keywords.

2. A Parameterized class need specify only the attributes of a
   Parameter whose values differ from those declared in
   superclasses; the other values will be inherited.  E.g. if Foo
   declares

    delta = Parameter(default=0.2) 

   the default value of 0.2 will override the 0.6 inherited from
   Bar, but the doc will be inherited from Bar.

3. The Parameter descriptor class can be subclassed to provide
   more complex behavior, allowing special types of parameters
   that, for example, require their values to be numbers in
   certain ranges, generate their values dynamically from a random
   distribution, or read their values from a file or other
   external source.

4. The attributes associated with Parameters provide enough
   information for automatically generating property sheets in
   graphical user interfaces, allowing Parameterized instances to
   be edited by users.

Note that Parameters can only be used when set as class attributes
of Parameterized classes. Parameters used as standalone objects,
or as class attributes of non-Parameterized classes, will not have
the behavior described here.

Nested Classes [hide private]
  __metaclass__
Metaclass allowing control over creation of Parameter classes.
Instance Methods [hide private]
 
__init__(self, default=None, doc=None, precedence=None, instantiate=False, constant=False, readonly=False, pickle_default_value=True)
Initialize a new Parameter object: store the supplied attributes.
source code
 
_set_instantiate(self, instantiate)
Constant parameters must be instantiated.
source code
 
__get__(self, obj, objtype)
Return the value for this Parameter.
source code
 
__set__(self, obj, val)
Set the value for this Parameter.
source code
 
__delete__(self, obj) source code
 
_set_names(self, attrib_name) source code
 
__getstate__(self)
All Parameters have slots, not a dict, so we have to support pickle and deepcopy ourselves.
source code
 
__setstate__(self, state) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  __classdoc = '\n An attribute descriptor for declaring para...
str(object) -> string
Properties [hide private]
  _attrib_name
  _internal_name
  constant
  default
  doc
  instantiate
  pickle_default_value
  precedence
  readonly

Inherited from object: __class__

Method Details [hide private]

__init__(self, default=None, doc=None, precedence=None, instantiate=False, constant=False, readonly=False, pickle_default_value=True)
(Constructor)

source code 

Initialize a new Parameter object: store the supplied attributes.

default: the owning class's value for the attribute represented by this Parameter.

precedence is a value, usually in the range 0.0 to 1.0, that allows the order of Parameters in a class to be defined (for e.g. in GUI menus). A negative precedence indicates a parameter that should be hidden in e.g. GUI menus.

default, doc, and precedence default to None. This is to allow inheritance of Parameter slots (attributes) from the owning-class' class hierarchy (see ParameterizedMetaclass).

In rare cases where the default value should not be pickled, set pickle_default_value=False (e.g. for file search paths).

Overrides: object.__init__

__get__(self, obj, objtype)

source code 

Return the value for this Parameter.

If called for a Parameterized class, produce that class's value (i.e. this Parameter object's 'default' attribute).

If called for a Parameterized instance, produce that instance's value, if one has been set - otherwise produce the class's value (default).

__set__(self, obj, val)

source code 

Set the value for this Parameter.

If called for a Parameterized class, set that class's value (i.e. set this Parameter object's 'default' attribute).

If called for a Parameterized instance, set the value of this Parameter on that instance (i.e. in the instance's __dict__, under the parameter's internal_name).

If the Parameter's constant attribute is True, only allows the value to be set for a Parameterized class or on uninitialized Parameterized instances.

If the Parameter's readonly attribute is True, only allows the value to be specified in the Parameter declaration inside the Parameterized source code. A read-only parameter also cannot be set on a Parameterized class.

Note that until we support some form of read-only object, it is still possible to change the attributes of the object stored in a constant or read-only Parameter (e.g. the left bound of a BoundingBox).


Class Variable Details [hide private]

__classdoc

str(object) -> string

Return a nice string representation of the object. If the argument is a string, the return value is the same object.

Value:
'''
    An attribute descriptor for declaring parameters.

    Parameters are a special kind of class attribute.  Setting a
    Parameterized class attribute to be a Parameter instance causes
    that attribute of the class (and the class\'s instances) to be
    treated as a Parameter.  This allows special behavior, including
    dynamically generated parameter values, documentation strings,
...