Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save travishen/f594610121eb78f8c8413e0823baf842 to your computer and use it in GitHub Desktop.

Select an option

Save travishen/f594610121eb78f8c8413e0823baf842 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ee258d3c",
"metadata": {},
"outputs": [],
"source": [
"class Example:\n",
" data = 'this is data attribute'\n",
" @property\n",
" def prop(self):\n",
" return 'this is property'"
]
},
{
"cell_type": "markdown",
"id": "14ffc5e7",
"metadata": {},
"source": [
"## Override of data attribute"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e6f52904",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"obj = Example()\n",
"\n",
"# This obj does not have any instance attributes\n",
"vars(obj) == obj.__dict__ == {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ec43c1d0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Override data attribute\n",
"obj.data = 'overrided'\n",
"obj.data == 'overrided' and vars(obj) == {'data': 'overrided'}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "34c76d21",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'this is data attribute'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Class data attribue still remains\n",
"Example.data"
]
},
{
"cell_type": "markdown",
"id": "ca592fd5",
"metadata": {},
"source": [
"## Override of property"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e9b465bc",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "can't set attribute",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-f8edd667f4b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Cannot set property without a setter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'overrided'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: can't set attribute"
]
}
],
"source": [
"obj = Example()\n",
"\n",
"# Cannot set property without a setter\n",
"obj.prop = 'overrided'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "80ff05a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'this is property'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Manually set the data attribute by patch the __dict__\n",
"obj.__dict__['prop'] = 'overrided'\n",
"# Getter of the property still exists\n",
"obj.prop"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "69274da2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'overrided'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Destroy the property, or use `del`\n",
"Example.prop = 'something else'\n",
"# obj.prop no longer a property, its value now came from the instance's attributes\n",
"obj.prop"
]
},
{
"cell_type": "markdown",
"id": "d255cdf8",
"metadata": {},
"source": [
"## Override attribute with created property"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "087e09fd",
"metadata": {},
"outputs": [],
"source": [
"obj = Example()\n",
"obj.val = 'this is instance data attribute'"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8f5b554c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'overrided by property'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Example.val = property(lambda self: 'overrided by property')\n",
"# Now it reads from property\n",
"obj.val"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "04bfce79",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'this is instance data attribute'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Destroy the property\n",
"del Example.val\n",
"# Now its read from instance attribute again\n",
"obj.val"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "310499bc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment