Skip to content

Instantly share code, notes, and snippets.

@jithinjk
Last active March 26, 2018 05:14
Show Gist options
  • Save jithinjk/c255186041323726bf23838b5640c2f8 to your computer and use it in GitHub Desktop.
Save jithinjk/c255186041323726bf23838b5640c2f8 to your computer and use it in GitHub Desktop.
new_types.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Student(name='Tommy Johnson', address='Main street', age=22, sex='M')"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import NamedTuple\n",
"\n",
"class Student(NamedTuple):\n",
" name: str\n",
" address: str\n",
" age: int\n",
" sex: str\n",
" \n",
"tommy = Student(name='Tommy Johnson', address='Main street', age=22, sex='M')\n",
"tommy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
},
{
"data": {
"text/plain": [
"'Tommy Johnson'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(isinstance(tommy, tuple))\n",
"tommy[0]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "__new__() missing 1 required positional argument: 'sex'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-c36c04942fdd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0msex\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'M'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mMaleStudent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Tommy Johnson'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maddress\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Main Street'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mage\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m22\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: __new__() missing 1 required positional argument: 'sex'"
]
}
],
"source": [
"class MaleStudent(Student):\n",
" sex: str = 'M'\n",
" \n",
"MaleStudent(name='Tommy Johnson', address='Main Street', age=22)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:tea_facts]",
"language": "python",
"name": "conda-env-tea_facts-py"
},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment