|
37 | 37 | "id": "bb4f57d7-b874-427f-a976-2b32375ee4bd", |
38 | 38 | "metadata": {}, |
39 | 39 | "source": [ |
40 | | - "The Static methods are created using the decorator `staticmethod`. " |
| 40 | + "The Static methods are created using the decorator `staticmethod`." |
41 | 41 | ] |
42 | 42 | }, |
43 | 43 | { |
44 | 44 | "cell_type": "code", |
45 | 45 | "execution_count": null, |
46 | 46 | "id": "e785758b-8ef9-4920-9fdf-5d0f0d9d8976", |
47 | | - "metadata": {}, |
| 47 | + "metadata": { |
| 48 | + "tags": [] |
| 49 | + }, |
48 | 50 | "outputs": [], |
49 | 51 | "source": [ |
50 | 52 | "class StaticMethodExample:\n", |
51 | 53 | " @staticmethod\n", |
52 | 54 | " def do_something():\n", |
53 | 55 | " " |
54 | 56 | ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 1, |
| 61 | + "id": "87f2b054-fbab-4bdb-b01c-ef66d5bdb869", |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "class Anime:\n", |
| 66 | + " list_of_animes = []\n", |
| 67 | + " \n", |
| 68 | + " def __init__(self, name, characters):\n", |
| 69 | + " self.name = name\n", |
| 70 | + " self.characters = characters\n", |
| 71 | + " Anime.list_of_animes.append(name)\n", |
| 72 | + " \n", |
| 73 | + " def introduce(self): # Instance Method.\n", |
| 74 | + " print(f\"Hey weebs! We are {self.name}. We have awesome characters: {self.characters} 🔥\")\n", |
| 75 | + " \n", |
| 76 | + " @classmethod \n", |
| 77 | + " def show_list_and_count_of_animes(cls): # Class Method.\n", |
| 78 | + " print(f\"Animes listed are: {cls.list_of_animes} which sums up to {len(cls.list_of_animes)}\")\n", |
| 79 | + " \n", |
| 80 | + " @staticmethod\n", |
| 81 | + " def temp():\n", |
| 82 | + " return Anime.list_of_animes, __class__.list_of_animes\n", |
| 83 | + " \n", |
| 84 | + " \n", |
| 85 | + " \n", |
| 86 | + " " |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "id": "2bed91ec-a06a-4918-b224-7b65f41f4121", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "a = Anime(\"one piece\", (\"luffy\",))\n", |
| 97 | + "b = Anime(\"naruto\", (\"naruto\", ))" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "id": "191cc8fb-0708-497c-b76d-d1e3d52118b1", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "| Method type | Can modify Class Attribute? | Can modify Method Attribute?|\n", |
| 106 | + "|----------------------|-------------------------------|-----------------------------|\n", |
| 107 | + "| Instance Method | ✅ | ✅ |\n", |
| 108 | + "| Class Method | ✅ | ❌ |\n", |
| 109 | + "| Static Method | ❌ | ❌ |" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "id": "a14d5dd8-438f-44b5-929c-9f6391c67ae3", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "```{note}\n", |
| 118 | + "Don't curse me saying, Hey! I can still modify class attributes from Static Method. Yes, we can absolutely do it using the class as `<ClassName>.<class_attribute>`, but we wouldn't be having class \n", |
| 119 | + "```" |
| 120 | + ] |
55 | 121 | } |
56 | 122 | ], |
57 | 123 | "metadata": { |
|
0 commit comments