2
from traits.api import Str
class Foo(HasTraits):
    a=Str
    b=Str()

Is there any behavioral difference between members 'a' and 'b'?

Are trait attributes always instance specific or can they be class members?

user3391229
  • 798
  • 9
  • 17

1 Answers1

5

There is no difference. MetaHasTraits (the metaclass behind HasTraits) will happily consume either form and implicitly instantiate the former without arguments.

Trait attributes are always instance attributes, not class attributes.

Robert Kern
  • 13,118
  • 3
  • 35
  • 32
  • That being said, I just found "add_class_trait()" : http://docs.enthought.com/traits/traits_user_manual/advanced.html#add-class-trait – user3391229 Oct 30 '15 at 04:25
  • In the same line of questioning, if 'a' is a member of a class, is there a difference between a=MyClass() and a=Instance(MyClass) and a=Instance(MyClass,()) ? – user3391229 Oct 30 '15 at 04:35
  • @user3391229 That should be a new question, not a comment. – Warren Weckesser Oct 30 '15 at 18:54
  • `add_class_trait()` is a `@classmethod` that will add a trait to a class such that all instances of that class will now have that trait as an instance attribute. This contrasts with `add_trait()` which is an `instancemethod` that adds the trait only to the instance you called it on and not other instances of the same class. In either case, the trait defines an instance attribute and not class attributes. – Robert Kern Oct 31 '15 at 18:37