trait metadata across instances

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

trait metadata across instances

Christopher Burns
We're currently changing our code to use traits and one of the primary
motivators is the power and flexibility that custom metadata allows
us.  However, I just discovered that changes to the metadata in one
instance of a traited attribute, changes the value of that metadata
across all instances.  The code below shows an example of this.  Am I
accessing the metadata incorrectly?  Is there a way around this or is
this the expected behavior?

import enthought.traits.api as traits

class Foo(traits.HasTraits):
    x = traits.Float(mandatory = True)

first = Foo()
second = Foo()

first_xt = first.trait('x')
second_xt = second.trait('x')

# XXX:  I would have expected these asserts to fail
assert first_xt is second_xt
first_xt.mandatory = False
assert second_xt.mandatory == False


Christopher Burns
Computational Infrastructure for Research Labs
10 Giannini Hall, UC Berkeley
Enthought-Dev mailing list
[hidden email]