Container Objects Enforcing Contents
Sean 'Shaleh' Perry
shalehperry at attbi.com
Wed Feb 13 19:02:16 EST 2002
On 13-Feb-2002 jim kraai wrote:
> Greetings,
>
> I'm looking for suggestions on making container objects which must,
> may, or must not allow particular kinds of container objects to be
> added.
>
> Any suggestions on a general approach for this?
Let's see .......
a constraints approach:
class Foo:
def __init__(self, item):
try:
# look for a method of item that must exist
except AttributeError:
raise ConstraintException
This way you could add only items which support a method or have a certain
attribute.
You could also try to enforce types by ensuring the type of item matches types
you know about. This often leads to a maintenance nightmare though.
More information about the Python-list
mailing list