_name_default for trait added with add_class_trait

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

_name_default for trait added with add_class_trait

Ryan Olf
Devs,

Traits added to a class with add_class_trait('name',trait) do not hook up properly to methods defined in the class with _name_default(). One can get around this by subclassing the trait and using get_default_value to hook up the right method, but this seems unnecessarily cumbersome.

An example of what I'm talking about is here:
----------
from traits.api import HasTraits, Int
class Test(HasTraits):
    #t = Int
    def _t_default(self):
        return 1
Test.add_class_trait('t', Int)

foo = Test()
print foo.t
__________
Output:  0  (1 is the desired output)

Is there a way to make this work that I'm missing, other than subclassing Int?

Ryan

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

Re: _name_default for trait added with add_class_trait

Jaidev Deshpande
Hi,

On Fri, Apr 5, 2013 at 12:14 PM, Ryan Olf <[hidden email]> wrote:

> Devs,
>
> Traits added to a class with add_class_trait('name',trait) do not hook up
> properly to methods defined in the class with _name_default(). One can get
> around this by subclassing the trait and using get_default_value to hook up
> the right method, but this seems unnecessarily cumbersome.
>
> An example of what I'm talking about is here:
> ----------
> from traits.api import HasTraits, Int
> class Test(HasTraits):
>     #t = Int
>     def _t_default(self):
>         return 1

_trait_default() methods and traits notifications work only on traits
that are explicitly defined, not on the ones that are added using
add_trait or add_class_trait.

> Test.add_class_trait('t', Int)
>
> foo = Test()
> print foo.t
> __________
> Output:  0  (1 is the desired output)
>
> Is there a way to make this work that I'm missing, other than subclassing
> Int?

I don't see how subclassing Int would help. What did you have in mind?

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



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

Re: _name_default for trait added with add_class_trait

Ryan Olf
Actually, certain trait notifications do seem to work for traits defined by add_class_trait. And subclassing works to get default behavior via overriding the get_default_value method. However, this is somewhat cumbersome. For example, the following example does what I want (and I use something like this in my code... though I'm using Instance and not Int). Is it worth filing a bug abut the _default behavior? If on_trait_change notifications are properly hooked up, it may not be too much extra trouble to hook up _default methods as well.

------------------------
from traits.api import HasTraits, Int, BaseInt, on_trait_change
class Test(HasTraits):
    #t = Int
    def _t_default(self):
        return 1

    @on_trait_change('t')
    def f(self):
        print "t changed"

class DefInt(BaseInt):
    def get_default_value(self):
        return (8, Test._t_default)
Test.add_class_trait('t', DefInt)

foo = Test()
print foo.t
foo.t = 2

---------------
output:
1
t changed



Ryan


On Mon, Apr 8, 2013 at 10:41 AM, Jaidev Deshpande <[hidden email]> wrote:
Hi,

On Fri, Apr 5, 2013 at 12:14 PM, Ryan Olf <[hidden email]> wrote:
> Devs,
>
> Traits added to a class with add_class_trait('name',trait) do not hook up
> properly to methods defined in the class with _name_default(). One can get
> around this by subclassing the trait and using get_default_value to hook up
> the right method, but this seems unnecessarily cumbersome.
>
> An example of what I'm talking about is here:
> ----------
> from traits.api import HasTraits, Int
> class Test(HasTraits):
>     #t = Int
>     def _t_default(self):
>         return 1

_trait_default() methods and traits notifications work only on traits
that are explicitly defined, not on the ones that are added using
add_trait or add_class_trait.

> Test.add_class_trait('t', Int)
>
> foo = Test()
> print foo.t
> __________
> Output:  0  (1 is the desired output)
>
> Is there a way to make this work that I'm missing, other than subclassing
> Int?

I don't see how subclassing Int would help. What did you have in mind?

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



--
JD
_______________________________________________
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