Traits reflection

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Traits reflection

Des.P
Given:

class A(HasTraits):
        pass

class B(HasTraits):
        l = List(A)

I would like to programatically find out (at class level, without making instances of A or B) that B has a trait 'l', whose type is a List(A).

I am trying:

l_trait = B.class_traits()['l']  # get trait
l_trait.is_trait_type(List) # returns True, encouraging...

But how do I go deeper to find out the trait A in the List(A)?

Thanks!

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Traits reflection

Simon Jagoe

This is how I tend to do it, if I really have to. If you change this to be:

>>> class A(HasTraits):
...         pass

>>> class B(HasTraits):
...         l = List(Instance(A))


Then you can get the following:

>>> l_trait = B.class_traits()['l'].handler

>>> isinstance(l_trait, List)

True

>>> instance_trait = l_trait.inner_traits()[0] # This is a tuple

>>> instance_trait.handler.klass is A

True

-- Sent from my phone
Simon Jagoe
Enthought Ltd
<a href="tel:%2B44%2079%20312%2011%20506" value="+447931211506" target="_blank">+44 79 312 11 506
[hidden email]

On 15 Sep 2012 08:26, "Des.P" <[hidden email]> wrote:
Given:

class A(HasTraits):
        pass

class B(HasTraits):
        l = List(A)

I would like to programatically find out (at class level, without making instances of A or B) that B has a trait 'l', whose type is a List(A).

I am trying:

l_trait = B.class_traits()['l']  # get trait
l_trait.is_trait_type(List) # returns True, encouraging...

But how do I go deeper to find out the trait A in the List(A)?

Thanks!

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev