HasTraits and Constructors

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

HasTraits and Constructors

Des.P
I am having trouble defining an ordinary __init__ constructor with positional parameters for a HasTraits subclass, as the TraitsUI system uses keywords for initialization. Is is possible to have positional constructor parameters and still use TraitsUI?

class Person
  name = Str
  def __init__(self, n):  ## some other magic needed?
        self.name = n
        ## some other magic here?

Thanks!

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

Re: HasTraits and Constructors

Pietro Berkes
It's rare to use constructors with traits, but if you must, you need to make sure you're passing the keyword arguments through:

def __init__(self, n, **traits):
    super(Person, self).__init__(**traits)
    self.name = n

P.

On Tue, Sep 4, 2012 at 5:41 PM, Des.P <[hidden email]> wrote:
I am having trouble defining an ordinary __init__ constructor with positional parameters for a HasTraits subclass, as the TraitsUI system uses keywords for initialization. Is is possible to have positional constructor parameters and still use TraitsUI?

class Person
  name = Str
  def __init__(self, n):  ## some other magic needed?
        self.name = n
        ## some other magic here?

Thanks!

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



--
Pietro Berkes
Scientific software developer
Enthought UK



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

Re: HasTraits and Constructors

Robert Kern
On Wed, Sep 5, 2012 at 10:00 AM, Pietro Berkes <[hidden email]> wrote:
> It's rare to use constructors with traits, but if you must, you need to make
> sure you're passing the keyword arguments through:
>
> def __init__(self, n, **traits):
>     super(Person, self).__init__(**traits)
>     self.name = n

If you are simply assigning the value to a trait, I recommend keeping
the name the same and providing a sensible default:

  def __init__(self, name='', **traits):
      super(Person, self).__init__(name=name, **traits)

> On Tue, Sep 4, 2012 at 5:41 PM, Des.P <[hidden email]> wrote:
>>
>> I am having trouble defining an ordinary __init__ constructor with
>> positional parameters for a HasTraits subclass, as the TraitsUI system uses
>> keywords for initialization. Is is possible to have positional constructor
>> parameters and still use TraitsUI?
>>
>> class Person
>>   name = Str
>>   def __init__(self, n):  ## some other magic needed?
>>         self.name = n
>>         ## some other magic here?
>>
>> Thanks!
>>
>> Desmond
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
>
> --
> Pietro Berkes
> Scientific software developer
> Enthought UK
>
>
>
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>



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