{"action":"created","issue":{"url":"https://api.github.com/repos/brython-dev/brython/issues/115","labels_url":"https://api.github.com/repos/brython-dev/brython/issues/115/labels{/name}","comments_url":"https://api.github.com/repos/brython-dev/brython/issues/115/comments","events_url":"https://api.github.com/repos/brython-dev/brython/issues/115/events","html_url":"https://github.com/brython-dev/brython/issues/115","id":55408346,"number":115,"title":"Adding properties to basic types","user":{"login":"kikocorreoso","id":1939575,"avatar_url":"https://avatars.githubusercontent.com/u/1939575?v=3","gravatar_id":"","url":"https://api.github.com/users/kikocorreoso","html_url":"https://github.com/kikocorreoso","followers_url":"https://api.github.com/users/kikocorreoso/followers","following_url":"https://api.github.com/users/kikocorreoso/following{/other_user}","gists_url":"https://api.github.com/users/kikocorreoso/gists{/gist_id}","starred_url":"https://api.github.com/users/kikocorreoso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kikocorreoso/subscriptions","organizations_url":"https://api.github.com/users/kikocorreoso/orgs","repos_url":"https://api.github.com/users/kikocorreoso/repos","events_url":"https://api.github.com/users/kikocorreoso/events{/privacy}","received_events_url":"https://api.github.com/users/kikocorreoso/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-25T10:07:28Z","updated_at":"2015-01-29T17:17:42Z","closed_at":"2015-01-29T17:07:52Z","body":"Is there a canonical way to add a property to a python type implemented in js?\r\n\r\nFor instance, yesterday I was trying to add some (Python) properties to number classes (*py_float.js, py_int.js, py_complex.js*) using the [builtin `property`](https://github.com/brython-dev/brython/blob/master/src/py_builtin_functions.js#L1139) and the following code wasn't working:\r\n\r\n```javascript\r\n$IntDict.numerator = property(function(self){return self})\r\n$IntDict.denominator = property(function(){return 1})\r\n```\r\n\r\nWhen I try to use the properties:\r\n```python\r\na = 2\r\nprint(a.numerator)\r\n```\r\n I get:\r\n```\r\n<property object>\r\n```\r\nInstead of the expected value `2`.\r\n\r\nI also tried to use js builtin way to define properties:\r\n\r\n```javascript\r\nObject.defineProperty($IntDict, \"numerator\", {\r\n    get: function(self) {return self},\r\n    set: undefined\r\n});\r\n```\r\n\r\nAnd the result was something like `int doesn't have attribute 'numerator'`."},"comment":{"url":"https://api.github.com/repos/brython-dev/brython/issues/comments/72064909","html_url":"https://github.com/brython-dev/brython/issues/115#issuecomment-72064909","issue_url":"https://api.github.com/repos/brython-dev/brython/issues/115","id":72064909,"user":{"login":"PierreQuentel","id":3447215,"avatar_url":"https://avatars.githubusercontent.com/u/3447215?v=3","gravatar_id":"","url":"https://api.github.com/users/PierreQuentel","html_url":"https://github.com/PierreQuentel","followers_url":"https://api.github.com/users/PierreQuentel/followers","following_url":"https://api.github.com/users/PierreQuentel/following{/other_user}","gists_url":"https://api.github.com/users/PierreQuentel/gists{/gist_id}","starred_url":"https://api.github.com/users/PierreQuentel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PierreQuentel/subscriptions","organizations_url":"https://api.github.com/users/PierreQuentel/orgs","repos_url":"https://api.github.com/users/PierreQuentel/repos","events_url":"https://api.github.com/users/PierreQuentel/events{/privacy}","received_events_url":"https://api.github.com/users/PierreQuentel/received_events","type":"User","site_admin":false},"created_at":"2015-01-29T17:17:42Z","updated_at":"2015-01-29T17:17:42Z","body":"Kiko,\r\n\r\nI propose the following for types with attribute `$native` set : \r\n- create descriptors like ordinary methods, eg \r\n\r\n```javascript\r\n$IntDict.numerator = function(self){return self}\r\n```\r\n\r\n- define an attribute `descriptor` for the class of the built-in type\r\n- set it to a Javascript object indexed by the names of the descriptors\r\n\r\n```javascript\r\nvar $IntDict = {__class__:$B.$type,\r\n    __name__:'int',\r\n    __dir__:$ObjectDict.__dir__,\r\n    toString:function(){return '$IntDict'},\r\n    $native:true,\r\n    descriptors:{numerator:true}\r\n}\r\n```\r\n\r\nI tested it with the above example, it works and should not cause performance issues, or very limited. It is implemented in the commit referenced above. Is it ok for you ?"}}