Skip to content

Instantly share code, notes, and snippets.

@eteq
Created December 3, 2014 00:32
Show Gist options
  • Select an option

  • Save eteq/3134913ecac69b93912f to your computer and use it in GitHub Desktop.

Select an option

Save eteq/3134913ecac69b93912f to your computer and use it in GitHub Desktop.
IPython notebook demonstrating how properties are different in new-style and old-style py 2.x classes
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:0f3ed789fd275adb4b0bb33d63e4fdd9866254f4742fd1f949f09014928d664b"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that this is run in py2.7"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class A:\n",
" def __init__(self,a ):\n",
" self.a = a\n",
" \n",
"class B(A):\n",
" @property\n",
" def a(self):\n",
" return self._a + 100\n",
" @a.setter\n",
" def a(self, val):\n",
" self._a = val\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"o=A(1)\n",
"o.a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"1"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"o=B(2)\n",
"o.a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"2"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class A(object):\n",
" def __init__(self,a ):\n",
" self.a = a\n",
" \n",
"class B(A):\n",
" @property\n",
" def a(self):\n",
" return self._a + 100\n",
" @a.setter\n",
" def a(self, val):\n",
" self._a = val"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"o=A(1)\n",
"o.a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"1"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"o=B(2)\n",
"o.a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"102"
]
}
],
"prompt_number": 9
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment