Skip to content

use of classVar. A good example to understand when we need classVar #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"id": "Tq7xqEeCdYAF"
},
Expand All @@ -11,9 +11,16 @@
"from dataclasses import dataclass"
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "htVhnhv6xdqe"
}
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 3,
"metadata": {
"id": "PnZmcPWKdjPi"
},
Expand Down Expand Up @@ -44,7 +51,16 @@
},
{
"cell_type": "code",
"execution_count": 23,
"source": [],
"metadata": {
"id": "ZC8IJDxr0P_G"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
Expand Down Expand Up @@ -74,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand All @@ -100,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
Expand Down Expand Up @@ -128,6 +144,63 @@
"print(john.weight)\n",
"print(American.national_language)"
]
},
{
"cell_type": "markdown",
"source": [
"A good example to show why we need ClassVar. A gas station have many pumps installed in it and all the pumps use the same gas tank. a gas tank contains the remaing fuel in it."
],
"metadata": {
"id": "-DcazKFlxfhd"
}
},
{
"cell_type": "code",
"source": [
"from typing import ClassVar\n",
"\n",
"@dataclass\n",
"class Gas_pump:\n",
" remaining_fuel: ClassVar[int] = 1000\n",
"\n",
" def dispense_fuel(self,fuel_in_liters:int):\n",
" self.fuel_in_liters = fuel_in_liters\n",
" Gas_pump.remaining_fuel -= fuel_in_liters\n",
" return f\"dispensed fuel: {self.fuel_in_liters}. Remaining fuel: {Gas_pump.remaining_fuel}\""
],
"metadata": {
"id": "NBVW5VQPxq6b"
},
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from os import stat\n",
"station1=Gas_pump()\n",
"station2=Gas_pump()\n",
"print(Gas_pump.dispense_fuel(station1,100))\n",
"print(Gas_pump.dispense_fuel(station2,100))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OWMHiA4B0ROA",
"outputId": "12c68eed-bed5-4555-c488-8bfe8cd787aa"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"dispensed fuel: 100. Remaining fuel: 900\n",
"dispensed fuel: 100. Remaining fuel: 800\n"
]
}
]
}
],
"metadata": {
Expand All @@ -144,4 +217,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
Loading