Customized Trait built from multiple inheritance

151 views Asked by At

I am trying to create a custom trait which represents a unipath.Path object. It seems advantageous to re-use the machinery provided by the File trait, so my thought was to use multiple inheritance.

from unipath import Path
from traits import File

class PathTrait(Path,File):
    pass

class A(HasTraits):
    p = PathTrait()

However, when i used this via A(p='/tmp/'), A.p does not have any methods associated with the Path object, as i would expect. Should i be implementing get and set methods?

1

There are 1 answers

2
aestrivex On

What do you expect A(p='/tmp') should do?

I can tell what you are trying to do but this statement should fail with TypeError if your code was correct. Instead of type error, you are replacing the variable P on the A object, which was previously an instance of PathTrait, with a string.

What you're trying to do is conceptually mixed up. File is a class which represents a trait object. Technically python allows you to extend this object, because python has very little type safety, but it doesn't mean your python class will now suddenly act like a trait.

To define custom traits you will need to use tools designed to operate with traits such as the Trait constructor.