Is it possible to dynamically add / remove traits from a class?

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

Is it possible to dynamically add / remove traits from a class?

Des.P
class A(HasTraits): pass

A.add_class_trait('x', ...)?
A.remove_class_trait('x')

Thanks!

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

Re: Is it possible to dynamically add / remove traits from a class?

Robert Kern
On Thu, Nov 1, 2012 at 9:46 PM, Des.P <[hidden email]> wrote:
> class A(HasTraits): pass
>
> A.add_class_trait('x', ...)?
> A.remove_class_trait('x')

The former works. The latter does not. One could write it, if you just
reverse the logic of HasTraits._add_class_trait(), but I really
recommend against it.

https://github.com/enthought/traits/blob/master/traits/has_traits.py#L1561

There certainly are use cases for adding class traits for some
advanced meta-programming factory-of-factories type things, but I
don't see a good use case for removing class traits from a class at
runtime. I would contend that it will cause more problems than it
would solve. There are usually far less disruptive ways to implement
what you want. What use case did you have in mind?

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

Re: Is it possible to dynamically add / remove traits from a class?

Des.P
Thanks, Robert.

On Nov 1, 2012, at 4:55 PM, Robert Kern wrote:

> On Thu, Nov 1, 2012 at 9:46 PM, Des.P <[hidden email]> wrote:
>> class A(HasTraits): pass
>>
>> A.add_class_trait('x', ...)?
>> A.remove_class_trait('x')
>
> The former works. The latter does not. One could write it, if you just
> reverse the logic of HasTraits._add_class_trait(), but I really
> recommend against it.
>
> https://github.com/enthought/traits/blob/master/traits/has_traits.py#L1561
>
> There certainly are use cases for adding class traits for some
> advanced meta-programming factory-of-factories type things, but I
> don't see a good use case for removing class traits from a class at
> runtime. I would contend that it will cause more problems than it
> would solve. There are usually far less disruptive ways to implement
> what you want. What use case did you have in mind?

Building a graphical-editor that incrementally builds, edits, and instantiates trait structures.

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