{"action":"created","issue":{"url":"https://api.github.com/repos/ipython/traitlets/issues/23","labels_url":"https://api.github.com/repos/ipython/traitlets/issues/23/labels{/name}","comments_url":"https://api.github.com/repos/ipython/traitlets/issues/23/comments","events_url":"https://api.github.com/repos/ipython/traitlets/issues/23/events","html_url":"https://github.com/ipython/traitlets/pull/23","id":84100092,"number":23,"title":"Simplify deferred creation of default values","user":{"login":"SylvainCorlay","id":2397974,"avatar_url":"https://avatars.githubusercontent.com/u/2397974?v=3","gravatar_id":"","url":"https://api.github.com/users/SylvainCorlay","html_url":"https://github.com/SylvainCorlay","followers_url":"https://api.github.com/users/SylvainCorlay/followers","following_url":"https://api.github.com/users/SylvainCorlay/following{/other_user}","gists_url":"https://api.github.com/users/SylvainCorlay/gists{/gist_id}","starred_url":"https://api.github.com/users/SylvainCorlay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SylvainCorlay/subscriptions","organizations_url":"https://api.github.com/users/SylvainCorlay/orgs","repos_url":"https://api.github.com/users/SylvainCorlay/repos","events_url":"https://api.github.com/users/SylvainCorlay/events{/privacy}","received_events_url":"https://api.github.com/users/SylvainCorlay/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-06-02T16:42:33Z","updated_at":"2015-06-09T00:20:33Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/ipython/traitlets/pulls/23","html_url":"https://github.com/ipython/traitlets/pull/23","diff_url":"https://github.com/ipython/traitlets/pull/23.diff","patch_url":"https://github.com/ipython/traitlets/pull/23.patch"},"body":"This is an attempt to simplify initialization of default values. \r\n\r\nThe behavior is slightly different in that now:\r\n```Python\r\nclass Foo(HasTraits):\r\n    bar = Int()\r\n\r\n# triggers a trait notification\r\nfoo = Foo()\r\nfoo.bar = 0\r\n\r\n# does not triggers a trait notification\r\nfoo = Foo(bar=0)\r\nfoo.bar = 0\r\n\r\n# accessing the uninitialized trait initiates the default value\r\nfoo = Foo()\r\nprint(foo.bar)  # prints 0\r\n```\r\n\r\nPreviously, the first example would not trigger a trait notification since 0 is equal to the default value which was initialized. I realize this is quite a change, but it really simplifies the code."},"comment":{"url":"https://api.github.com/repos/ipython/traitlets/issues/comments/110179624","html_url":"https://github.com/ipython/traitlets/pull/23#issuecomment-110179624","issue_url":"https://api.github.com/repos/ipython/traitlets/issues/23","id":110179624,"user":{"login":"takluyver","id":327925,"avatar_url":"https://avatars.githubusercontent.com/u/327925?v=3","gravatar_id":"","url":"https://api.github.com/users/takluyver","html_url":"https://github.com/takluyver","followers_url":"https://api.github.com/users/takluyver/followers","following_url":"https://api.github.com/users/takluyver/following{/other_user}","gists_url":"https://api.github.com/users/takluyver/gists{/gist_id}","starred_url":"https://api.github.com/users/takluyver/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/takluyver/subscriptions","organizations_url":"https://api.github.com/users/takluyver/orgs","repos_url":"https://api.github.com/users/takluyver/repos","events_url":"https://api.github.com/users/takluyver/events{/privacy}","received_events_url":"https://api.github.com/users/takluyver/received_events","type":"User","site_admin":false},"created_at":"2015-06-09T00:20:33Z","updated_at":"2015-06-09T00:20:33Z","body":"I think we can avoid the behaviour change if, in `__set__`, we change this:\r\n\r\n```python\r\ntry:\r\n    old_value = obj._trait_values[self.name]\r\nexcept KeyError:\r\n    old_value = Undefined\r\n```\r\n\r\nto this:\r\n\r\n```python\r\ntry:\r\n    old_value = obj._trait_values[self.name]\r\nexcept KeyError:\r\n    old_value = self.default_value\r\n```\r\n\r\n`self.default_value` is `Undefined` in cases where there's no obvious default, but it is e.g. `0` for an integer if no default was supplied."}}