On 2009-06-12 09:05, Paul LaFollette wrote:
Kind people,

Using Python 3.0 on a Gatesware machine (XP).
I am building a class in which I want to constrain the types that can
be stored in various instance variables.  For instance, I want to be
certain that self.loc contains an int.  This is straightforward (as
long as I maintain the discipline of changing loc through a method
rather than just twiddling it directly.

You may want to consider using Enthought's Traits package (disclosure: I work for Enthought).

  http://pypi.python.org/pypi/Traits/

Type-checking attributes is not my favorite feature of Traits (I try to avoid using this feature if I can avoid it), but it is designed for precisely your use cases.


In [2]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:from enthought.traits.api import Float, HasTraits, Instance, Int, This
:
:class FancyClass(HasTraits):
:    """ An example traited class.
:    """
:    x = Float(10.0)
:    y = Int(-20)
:
:class Node(HasTraits):
:    """ A node in a single-linked list.
:    """
:
:    # The location of this Node.
:    loc = Int(0)
:
:    # The next Node. This() is a special trait that accepts instances of
:    # this class, or None.
:    next = This()
:
:    # The data attached to the Node. It will accept instances of FancyClass
:    # or None.
:    data = Instance(FancyClass)
:
:
:--

In [3]: n = Node()

In [4]: n.next

In [5]: n.data

In [6]: n.loc
Out[6]: 0

In [7]: fc = FancyClass(x=15.0)

In [8]: fc.x
Out[8]: 15.0

In [9]: fc.y
Out[9]: -20

In [10]: fc.y = 'a string'
---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

/Users/rkern/svn/et/Traits/enthought/traits/trait_handlers.pyc in error(self, object, name, value)
    173         """
174 raise TraitError( object, name, self.full_info( object, name, value ),
--> 175                           value )
    176
    177     def arg_error ( self, method, arg_num, object, name, value ):

TraitError: The 'y' trait of a FancyClass instance must be an integer, but a value of 'a string' <type 'str'> was specified.

In [11]: n.data = fc

In [12]: n.data = None

In [13]: n.data = n
---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

/Users/rkern/svn/et/Traits/enthought/traits/trait_handlers.pyc in error(self, object, name, value)
    173         """
174 raise TraitError( object, name, self.full_info( object, name, value ),
--> 175                           value )
    176
    177     def arg_error ( self, method, arg_num, object, name, value ):

TraitError: The 'data' trait of a Node instance must be a FancyClass or None, but a value of <__main__.Node object at 0x17999c0> <class '__main__.Node'> was specified.

In [14]: n.next = Node(loc=1)

In [15]: n.next
Out[15]: <__main__.Node at 0x1892e70>

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to