enthought / enthought/traits

Overriding Default Values in a Subclass for Array

Open
#531 2 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Hello,

I am trying to override the default value of an Array trait in a subclass but it does not seem to work as explained in the docs, see from this modified example:
```python
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.

# override_default.py -- Example of overriding a default value for
# a trait attribute in a subclass

from traits.api import HasTraits, Range, Str, Array

# --[Code]----------------------------------------------------------------------
# Example of overriding a default value for a trait in a subclass:

# Define the base class:
class Employee(HasTraits):

name = Str
salary_grade = Range(value=1, low=1, high=10)
normal = Array(shape=(3,), dtype="float", value=(0.0, 0.0, 1.0))

# Define a subclass:
class Manager(Employee):

# Override the default value for the inherited 'salary_grade' trait:
salary_grade = 5
normal = (1., 0., 0.)

# --[Example*]------------------------------------------------------------------
# Create an employee and display its initial contents:
joe = Employee(name="Joe")
joe.print_traits()

# Now do the same thing for a manager object:
mike = Manager(name="Mike")
mike.print_traits()
```

This will produce the output (python 3.7.3, traits 5.1.2, anaconda):
```
name: 'Joe'
normal: array([0., 0., 1.])
salary_grade: 1
name: 'Mike'
normal:
salary_grade: 5
```

Casting the default value with a numpy array does not work too.

If it's not a bug, how should I work around it?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.