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

Type ParameterizedMetaclass

source code

object --+    
         |    
      type --+
             |
            ParameterizedMetaclass

The metaclass of Parameterized (and all its descendents).

The metaclass overrides type.__setattr__ to allow us to set Parameter values on classes without overwriting the attribute descriptor. That is, for a Parameterized class of type X with a Parameter y, the user can type X.y=3, which sets the default value of Parameter y to be 3, rather than overwriting y with the constant value 3 (and thereby losing all other info about that Parameter, such as the doc string, bounds, etc.).

The __init__ method is used when defining a Parameterized class, usually when the module where that class is located is imported for the first time. That is, the __init__ in this metaclass initializes the class object, while the __init__ method defined in each Parameterized class is called for each new instance of that class.

Additionally, a class can declare itself abstract by having an attribute __abstract set to True. The 'abstract' attribute can be used to find out if a class is abstract or not.

Instance Methods [hide private]
 
__init__(mcs, name, bases, dict_)
Initialize the class object (not an instance of the class, but the class itself).
source code
 
__is_abstract(mcs)
Return True if the class has an attribute __abstract set to True.
source code
 
__setattr__(mcs, attribute_name, value)
Implements 'self.attribute_name=value' in a way that also supports Parameters.
source code
 
__param_inheritance(mcs, param_name, param)
Look for Parameter values in superclasses of this Parameterized class.
source code
 
get_param_descriptor(mcs, param_name)
Goes up the class hierarchy (starting from the current class) looking for a Parameter class attribute param_name.
source code

Inherited from type: __call__, __cmp__, __delattr__, __getattribute__, __hash__, __new__, __repr__, __subclasses__, mro

Inherited from object: __reduce__, __reduce_ex__, __str__

Properties [hide private]
  abstract
Return True if the class has an attribute __abstract set to True.

Inherited from type: __base__, __bases__, __basicsize__, __dictoffset__, __flags__, __itemsize__, __mro__, __name__, __weakrefoffset__

Inherited from object: __class__

Method Details [hide private]

__init__(mcs, name, bases, dict_)
(Constructor)

source code 

Initialize the class object (not an instance of the class, but the class itself).

Initializes all the Parameters by looking up appropriate default values; see __param_inheritance().

Overrides: object.__init__

__is_abstract(mcs)

source code 
Return True if the class has an attribute __abstract set to True. Subclasses will return False unless they themselves have __abstract set to true. This mechanism allows a class to declare itself to be abstract (e.g. to avoid it being offered as an option in a GUI), without the "abstract" property being inherited by its subclasses (at least one of which is presumably not abstract).

__setattr__(mcs, attribute_name, value)

source code 

Implements 'self.attribute_name=value' in a way that also supports Parameters.

If there is already a descriptor named attribute_name, and that descriptor is a Parameter, and the new value is not a Parameter, then call that Parameter's __set__ method with the specified value.

In all other cases set the attribute normally (i.e. overwrite the descriptor). If the new value is a Parameter, once it has been set we make sure that the value is inherited from Parameterized superclasses as described in __param_inheritance().

Overrides: object.__setattr__

__param_inheritance(mcs, param_name, param)

source code 

Look for Parameter values in superclasses of this Parameterized class.

Ordinarily, when a Python object is instantiated, attributes not given values in the constructor will inherit the value given in the object's class, or in its superclasses. For Parameters owned by Parameterized classes, we have implemented an additional level of default lookup, should this ordinary lookup return only None.

In such a case, i.e. when no non-None value was found for a Parameter by the usual inheritance mechanisms, we explicitly look for Parameters with the same name in superclasses of this Parameterized class, and use the first such value that we find.

The goal is to be able to set the default value (or other slots) of a Parameter within a Parameterized class, just as we can set values for non-Parameter objects in Parameterized classes, and have the values inherited through the Parameterized hierarchy as usual.

get_param_descriptor(mcs, param_name)

source code 
Goes up the class hierarchy (starting from the current class) looking for a Parameter class attribute param_name. As soon as one is found as a class attribute, that Parameter is returned along with the class in which it is declared.

Property Details [hide private]

abstract

Return True if the class has an attribute __abstract set to True. Subclasses will return False unless they themselves have __abstract set to true. This mechanism allows a class to declare itself to be abstract (e.g. to avoid it being offered as an option in a GUI), without the "abstract" property being inherited by its subclasses (at least one of which is presumably not abstract).
Get Method:
__is_abstract(mcs) - Return True if the class has an attribute __abstract set to True.