Utility Classes :: HasAttr

HasAttr() is used when you don't care about the type of the object, per se, only that it implements a given interface.

@accepts(HasAttr(['foo', 'bar'])

In order for an object to pass the above signature, it must have both a foo and bar attribute, regardless of what type they are.

The following signature requires that the object have foo and bar attribute, both of which must be ints.

@accepts(HasAttr({'foo':int, 'bar':int})

These two modes of expression, named and typed, can be mixed. The following signature requires that the object have a foo attribute, which may be of any type, and a bar attribute, which must be callable:

@accepts(HasAttr(['foo'], {'bar': IsCallable()})

Note that if you specify the same attribute in both the named and typed modes, as in the following example, the typed mode will take precedence:

HasAttr(['foo', 'bar']. {'foo': IsCallable()})

is equivalent to

HasAttr(['bar']. {'foo': IsCallable()})

Details

Valid XHTML 1.0 Transitional