{ "cells": [ { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "class Dog:\n", " name = \"penny\"\n", "\n", "dog = Dog()\n", "\n", "\n", "class Person(object):\n", " name = \"donald\"\n", " username = \"miguire\"\n", " age = 23\n", " pet = dog\n", " \n", " def access(self, attr):\n", " return getattr(self, attr)\n", " \n", " def really(self, attr):\n", " if getattr(self, attr) < 10:\n", " print(\"Hello youngster\")\n", " else:\n", " print(\"Khulu\")\n", " \n", " def pet_name(self):\n", " return getattr(getattr(self, 'pet'), 'name')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "p = Person()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.access(\"age\")" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'donald'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.access(\"name\")" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'miguire'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.access(\"username\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Khulu\n" ] } ], "source": [ "p.really(\"age\")" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'penny'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.pet.name" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'penny'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.pet_name()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'user_type': 55}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{**{ 'user_type':444 }, **{ 'user_type': 55 }}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "h = {\n", " 'hu': \"hh\",\n", " 'hg': \"ht\",\n", " 'hz': \"hy\",\n", "}" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'09'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mh\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'09'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: '09'" ] } ], "source": [ "h['09']" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(0, int)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "genders = [\"Male\", \"Female\", \"Missing\", \"Trans Gender\"]" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Trans Gender'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "genders[3]" ] }, { "cell_type": "code", "execution_count": null, "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }